Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

283
Visualizações
How to solve Exception in thread "main" java.lang.NullPointerException when copying files/directories

I have this code sample to Copy files and directories from one location to another. But this exception keep showing every time I run the program. Can anyone please help me find out the error?

Exception in thread "main" java.lang.NullPointerException

public void listFilesAndFilesSubDirectories(String directoryName) throws FileNotFoundException, IOException {
    File directory = new File(directoryName);
    //get all the files from a directory
    File[] fList = directory.listFiles();
    for (File file : fList) {
        if (file.isFile()) {
            //System.out.println(file.getAbsolutePath());
            System.out.println("File");

            File source = new File("C:\\Users\\Core i3\\Desktop\\Check me\\" + file);
            File dest = new File("C:\\Users\\Core i3\\Desktop\\New folder");
            System.out.println(source);

            InputStream is = null;
            OutputStream os = null;
            try {
                is = new FileInputStream(source);
                os = new FileOutputStream(dest);
                byte[] buffer = new byte[1024];
                int length;
                while ((length = is.read(buffer)) > 0) {
                    os.write(buffer, 0, length);
                }
            } finally {
                is.close();
                os.close();
            }

        } else if (file.isDirectory()) {
            listFilesAndFilesSubDirectories(file.getAbsolutePath());

            System.out.println("Directory");

            File source = new File("C:\\Users\\Core i3\\Desktop\\Check me\\" + file);
            File dest = new File("C:\\Users\\Core i3\\Desktop\\New folder");

            InputStream is = null;
            OutputStream os = null;
            try {
                is = new FileInputStream(source);
                os = new FileOutputStream(dest);
                byte[] buffer = new byte[1024];
                int length;
                while ((length = is.read(buffer)) > 0) {
                    os.write(buffer, 0, length);
                }
            } finally {
                is.close();
                os.close();
            }

        }
    }

}
over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda