title: Check if folder is empty without reference issues
body: <p>The code below sorts an array of files that it reads from a directory. The only problem is that if the folder is empty then I get a null pointer exception. I have tried a few different ways of checking first if the folder is empty and then executing the below code, but they all used if statements which took this code out of scope. Is there a way I could check if the directory is empty and if it is skip over this code?</p>
  
  <pre><code>//sort array listOfFiles by time (oldest to newest)
      File folder = new File(dbBackupLocation + "/" + dbHost);
      File[] listOfFiles = folder.listFiles();
      Arrays.sort(listOfFiles, new Comparator&lt;File&gt;() {
          @Override
                      public int compare(File f1, File f2) {
              return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
          }
      });
  </code></pre>
  
  <p>Edit: NullPointerException on line <code>Arrays.sort(listOfFiles, new Comparator&lt;File&gt;() {</code></p>
  
  <p>I used Elliott Frisch code:</p>
  
  <pre><code>File folder = new File(dbBackupLocation + "/" + dbHost);
      File[] listOfFiles = folder.listFiles();
      if (listOfFiles != null &amp;&amp; listOfFiles.length &gt; 0) {
          // The array isn't empty.
          Arrays.sort(listOfFiles, new Comparator&lt;File&gt;() {
              @Override
              public int compare(File f1, File f2) {
                  return Long.valueOf(f1.lastModified())
                          .compareTo(f2.lastModified());
              }
          });
      }
  </code></pre>
  
  <p>But when I try to use listOfFiles like: <code>for(File s : listOfFiles) { i++; }</code> I am apparently dereferencing it? </p>