NullPointerException when showing JFileChooser

Posted by Geo on Stack Overflow See other posts from Stack Overflow or by Geo
Published on 2010-06-15T16:53:01Z Indexed on 2010/06/15 17:12 UTC
Read the original article Hit count: 1183

Filed under:
|
|

I show a JFileChooser with this snippet:

public File getDestination() {
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int option = chooser.showSaveDialog(null);
    if(option == JFileChooser.APPROVE_OPTION) {
        return chooser.getSelectedFile().getAbsolutePath();
    }
    return new File(".");
}

Usually, the first time it's showed, it displays & works correctly. The second time, it will always throw this exception:

Exception in thread "Basic L&F File Loading Thread" java.lang.NullPointerException
    at sun.awt.shell.Win32ShellFolder2.pidlsEqual(Unknown Source)
    at sun.awt.shell.Win32ShellFolder2.equals(Unknown Source)
    at sun.awt.shell.Win32ShellFolderManager2.isFileSystemRoot(Unknown Source)
    at sun.awt.shell.ShellFolder.isFileSystemRoot(Unknown Source)
    at javax.swing.filechooser.FileSystemView.isFileSystemRoot(Unknown Source)
    at javax.swing.filechooser.WindowsFileSystemView.isTraversable(Unknown Source)
    at javax.swing.JFileChooser.isTraversable(Unknown Source)
    at javax.swing.plaf.basic.BasicDirectoryModel$LoadFilesThread.run0(Unknown Source)
    at javax.swing.plaf.basic.BasicDirectoryModel$LoadFilesThread.run(Unknown Source)

Java -version says:

java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing)

And the thread I found here says I should downgrade the Java version. Should I follow their advice, or is there something I could have done wrong?

© Stack Overflow or respective owner

Related posts about java

Related posts about swing