title: NullPointerException when recursively copying files in Java
body: <p>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.</p>
  <p>Here is the code I wrote:</p>
  <pre class="lang-java prettyprint-override"><code>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&lt;1024){
                  System.out.println(&quot;Total of &quot; + size1 + &quot;B&quot; + &quot; were copied&quot;);
              }
              if(size1&gt;1024){
                  System.out.println(&quot;Total of &quot; + size1/1024 + &quot;KB&quot; + &quot; were copied&quot;);
              }
              if(size1&gt;1e-6){
                  System.out.println(&quot;Total of &quot; + size1/1024/1024 + &quot;MB&quot; + &quot; were copied&quot;);
              }
          }
      }
  
      static void recursiveCopy(File file, File Destination){
          if(file.isFile()){
              try {
                  System.out.println(&quot;&gt;Started: &quot; + file.getAbsolutePath());
                  copyFile(file, Destination);
                  System.out.println(&quot;&gt;Finished FILE : &quot; + file.getAbsolutePath());
                  return;
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          for(File sub:file.listFiles()){
              String indent2 = &quot;&quot;;
              System.out.println( indent2+ &quot;&gt;Started: &quot; + sub.getAbsolutePath());
              recursiveCopy(sub, Destination);
              indent2+= &quot;  &quot;;
          }
      }
  
      static void copyAll(File[] listOfFiles, File dest) throws IOException{
          for(File f:listOfFiles){
              recursiveCopy(f, dest);
              System.out.println(&quot;        &gt;Finished FOLDER: &quot; + f.getAbsolutePath());
          }
      }
  
      public static void main(String[] args) throws IOException {
          File[] listOfFiles = new File[] {new File(&quot;resources/docs/books&quot;),
                                           new File(&quot;resources/docs/books/some citations.txt&quot;)};
          copyAll(listOfFiles, new File(&quot;dest&quot;));
      }
  </code></pre>
  <p>It throws the <code>NullPointerException</code> with this message:</p>
  <blockquote>
  <p>Exception in thread &quot;main&quot; java.lang.NullPointerException: Cannot read the array length because &quot; &lt;local2&gt; &quot; is null</p>
  </blockquote>
  <p>The issue starts with the line <code>for(File sub:file.listFiles())</code></p>
  <p>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?</p>