Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5086412

File.listFiles() unexpectedly throws NullPointerException on Windows virtual folders (win)

XMLWordPrintable

    • Cause Known
    • x86
    • windows_2000, windows_xp

      Name: rmT116609 Date: 08/12/2004


      FULL PRODUCT VERSION :
      java version "1.4.2_05"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_05-b04)
      Java HotSpot(TM) Client VM (build 1.4.2_05-b04, mixed mode)

      java version "1.5.0-beta2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b51)
      Java HotSpot(TM) Client VM (build 1.5.0-beta2-b51, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      java.io.File#isDirectory reports true for Windows virtual folders such as "My Computer" but java.io.File#listFiles returns null when documentation indicates that it should not be null because it is a directory.

      The documentation I am referring to is in the JavaDoc for listFiles:
      "The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directoryM-^OÀ»"

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Use a Windows XP machine logged in as a limited user.
      2. Compile the source code.
      3. Run the resulting class and monitor output.
      4. In the JFileChooser, go up one directory from the inital directory which is your "My Documents" folder. The resulting Folder is the Desktop pseudo-folder.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No NullPointerException.
      ACTUAL -
      NullPointerException

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "Basic L&F File Loading Thread" java.lang.NullPointerException
              at listFilesBug.accept(listFilesBug.java:17)
              at javax.swing.JFileChooser.accept(Unknown Source)
              at javax.swing.plaf.basic.BasicDirectoryModel$LoadFilesThread.run(Unknown Source)

      With J2SE 1.4.2_05:

      java.lang.NullPointerException
              at listFilesBug.accept(listFilesBug.java:17)
              at javax.swing.JFileChooser.accept(JFileChooser.java:1536)
              at javax.swing.plaf.basic.BasicDirectoryModel$LoadFilesThread.run(BasicD
      irectoryModel.java:204)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.File;
      import javax.swing.JFileChooser;
      import javax.swing.filechooser.FileFilter;

      public class listFilesBug extends FileFilter implements java.io.FileFilter {
          public listFilesBug() {
              super();
          }

          public boolean accept(File f) {
              final boolean acceptableFile =
                  !f.isDirectory() && f.getName().endsWith(".java");
              System.out.println(f);
              System.out.println("is a directory according to Java if the following is true.");
              System.out.println(f.isDirectory());
              System.out.println(f);
              final boolean acceptableDir =
                  f.isDirectory() && f.listFiles(this).length > 0;
              final boolean acceptable = acceptableFile || acceptableDir;
              return acceptable;
          }
          
          public String getDescription() {
              return "";
          }
          
          public static void main(String[] args) {
              final JFileChooser fileChooser = new JFileChooser();
              final FileFilter filter = new listFilesBug();
              fileChooser.setFileFilter(filter);
              fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
              fileChooser.setAcceptAllFileFilterUsed(false);
              final int returnVal = fileChooser.showOpenDialog(null);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Change acceptableDir boolean value to f.isDirectory() && f.listFiles(this) != null && f.listFiles(this).length > 0;
      (Incident Review ID: 296814)
      ======================================================================

            tr Tejesh R
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: