JDK-5086412 : File.listFiles() unexpectedly throws NullPointerException on Windows virtual folders (win)
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.0,1.4.2
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_2000,windows_xp
  • CPU: x86
  • Submitted: 2004-08-12
  • Updated: 2018-09-05
Related Reports
Duplicate :  
Description
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) 
======================================================================

Comments
EVALUATION The issue is that JFileChooser must allow the user to navigate both file system folders and non-file system folders such as "My Computer". As part of the implementation java.io.File is extended as sun.awt.shell.ShellFolder. There appears to be some cases where it doesn't override the implementation and as a result the java.io.File implementation is provided with something that isn't really a file. For example, attempts to get canonical path will fail and throw IOE. In this bug report, the issue is that listFiles(FilenameFilter) is provided with the name of a non-file system folder. listFiles doesn't have any way to report errors and returns null that causes the NPE that the submitter observes. I suggest we move this bug to java/classes_swing. It looks like Win32ShellFolder2 needs to override listFiles(FilenameFilter) and deal the special Windows non-file system folders.
26-10-2005

EVALUATION Interesting bug. The test case should have its swing dependencies stripped. What is the actual name of the "virtual folder" file on XP? The test should have printed that out, but I don't see it in the cited output. ###@###.### 2004-08-12
12-08-2004