We have the following exercise: Given a list of directories and files followed by the destination copy all these directories (recursively) and files into the destination.
Here is the code I wrote:
public static void copyFile(File source, File dest) throws IOException {
    //use DataOutputStream for its method size() to measure amount of bytes copied
    try(FileInputStream is = new FileInputStream(source);
        FileOutputStream os = new FileOutputStream(dest);
        DataOutputStream dos = new DataOutputStream(os)) {
            byte[] buffer = new byte[1000];
            int res;
            while ((res = is.read(buffer)) !=-1) {
                dos.write(buffer, 0, res);
            }
            int size1 = dos.size();
            if(size1<1024){
                System.out.println("Total of " + size1 + "B" + " were copied");
            }
            if(size1>1024){
                System.out.println("Total of " + size1/1024 + "KB" + " were copied");
            }
            if(size1>1e-6){
                System.out.println("Total of " + size1/1024/1024 + "MB" + " were copied");
            }
        }
    }
    static void recursiveCopy(File file, File Destination){
        if(file.isFile()){
            try {
                System.out.println(">Started: " + file.getAbsolutePath());
                copyFile(file, Destination);
                System.out.println(">Finished FILE : " + file.getAbsolutePath());
                return;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        for(File sub:file.listFiles()){
            String indent2 = "";
            System.out.println( indent2+ ">Started: " + sub.getAbsolutePath());
            recursiveCopy(sub, Destination);
            indent2+= "  ";
        }
    }
    static void copyAll(File[] listOfFiles, File dest) throws IOException{
        for(File f:listOfFiles){
            recursiveCopy(f, dest);
            System.out.println("        >Finished FOLDER: " + f.getAbsolutePath());
        }
    }
    public static void main(String[] args) throws IOException {
        File[] listOfFiles = new File[] {new File("resources/docs/books"),
                                         new File("resources/docs/books/some citations.txt")};
        copyAll(listOfFiles, new File("dest"));
    }
It throws the NullPointerException with this message:
Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because " <local2> " is null
The issue starts with the line for(File sub:file.listFiles())
Does anyone know how I can resolve this issue? All the files and directories exist so I don't know which element of array could possibly be null. Perhaps there is something else?
 
     
    