显示JFileChooser时出现NullPointerException

时间:2010-06-15 16:53:02

标签: java swing nullpointerexception

我用这个片段展示了一个JFileChooser:

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(".");
}

通常,它第一次显示时,显示&工作正常。第二次,它将始终抛出此异常:

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说:

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)

我找到的帖子here说我应该降级Java版本。我应该听从他们的意见,还是有什么我可以做错的?

2 个答案:

答案 0 :(得分:2)

这在build 1.6u21

中得到修复

见这里: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6940843

答案 1 :(得分:1)

这看起来真的是版本问题/回归。 所以答案就是:遵循他们的建议,你没有做错任何事情;)