title: Getting null pointer exception in list Files method?
body: <p>I am trying to retrieve all <code>DocumentFile[]</code> using <code>listFiles()</code> method.But getting a null pointer exception how to eliminate this error and still get the files.
  This is the code which searches sd card root directory which is being passed as  <code>documentfile</code> in method parameter.</p>
  
  <pre><code>  private List&lt;DocumentFile&gt; getListFiles2(DocumentFile docfile) {
          List&lt;DocumentFile&gt; inFiles = new ArrayList&lt;&gt;();
          Queue&lt;DocumentFile&gt; files = new LinkedList&lt;&gt;();
  
  
  //  check error arising here
          files.addAll(Arrays.asList(docfile.listFiles()));
  
  
          while (!files.isEmpty()) {
              DocumentFile file = files.remove();
              if (file.isDirectory()) {
                  files.addAll(Arrays.asList(file.listFiles()));
  
              } else if (file.getName().endsWith(".bmp")
  || file.getName().endsWith(".gif")
                      || file.getName().endsWith(".jpg") || file.getName().endsWith(".jpeg")
                      || file.getName().endsWith(".png") 
             ) {
                  inFiles.add(file);
  
              }
          }
          return inFiles;
      }
  </code></pre>