title: Why can File.listFiles return null when called on a directory?
body: <p>I'm creating an Android app, and I want to list the files in a directory. I do this by calling</p>
  
  <pre><code>File[] files = path.listFiles(new CustomFileFilter());
  </code></pre>
  
  <p><code>path</code> is a <code>File</code> object, which is created by calling</p>
  
  <pre><code>File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
  </code></pre>
  
  <p>When I then try to get the length of the <code>files</code> array by calling</p>
  
  <pre><code>int length = files.length;
  </code></pre>
  
  <p>This line gives me a <code>NullPointerException</code>, because <code>files</code> is null.</p>
  
  <p>I have checked if the <code>path</code> which I try to list the files in exists by calling</p>
  
  <pre><code>System.out.println("Path exists: " + path.exists());
  </code></pre>
  
  <p>And when I run the app, it prints</p>
  
  <pre><code>Path exists: true
  </code></pre>
  
  <p>in the Android Studio console, so the directory exists.</p>
  
  <p>I've also printed the path name, which is</p>
  
  <pre><code>/storage/emulated/0/Download
  </code></pre>
  
  <p>So the <code>path</code> is a directory, and not a file.</p>
  
  <p>I have no idea about why I'm getting a <code>NullPointerException</code>, because the <code>path</code> is a directory.</p>
  
  <p><strong>EDIT</strong>: The <code>CustomFileFilter</code> class looks like this:</p>
  
  <pre><code>public class CustomFileFilter implements FileFilter {
  
      // Determine if the file should be accepted
      @Override
      public boolean accept(File file) {
          // If the file isn't a directory
          if(file.isDirectory()) {
              // Accept it
              return true;
          } else if(file.getName().endsWith("txt")) {
              // Accept it
              return true;
          }
          // Don't accept it
          return false;
      }
  }
  </code></pre>