Java JFileChooser失败

Java JFileChooser失败,java,swing,Java,Swing,在我的代码中的某一点上,我有这个代码 void selectRoot() { JFileChooser ch = new JFileChooser(); ch.showOpenDialog((JFrame)this); } 这会引发异常: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at java.io.Win32FileSystem.normaliz

在我的代码中的某一点上,我有这个代码

void selectRoot() {
        JFileChooser ch = new JFileChooser();
        ch.showOpenDialog((JFrame)this);
    }
这会引发异常:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.io.Win32FileSystem.normalize(Win32FileSystem.java:164)
    at java.io.Win32FileSystem.getUserPath(Win32FileSystem.java:296)
    at java.io.Win32FileSystem.resolve(Win32FileSystem.java:312)
    at java.io.File.getAbsolutePath(File.java:501)
    at sun.awt.shell.Win32ShellFolder2.<init>(Win32ShellFolder2.java:291)
    at sun.awt.shell.Win32ShellFolderManager2.createShellFolderFromRelativePIDL(Win32ShellFolderManager2.java:66)
    at sun.awt.shell.Win32ShellFolderManager2.createShellFolder(Win32ShellFolderManager2.java:56)
    at sun.awt.shell.Win32ShellFolderManager2.getRecent(Win32ShellFolderManager2.java:114)
    at sun.awt.shell.Win32ShellFolderManager2.get(Win32ShellFolderManager2.java:251)
    at sun.awt.shell.ShellFolder.get(ShellFolder.java:227)
    at sun.swing.WindowsPlacesBar.<init>(WindowsPlacesBar.java:64)
    at com.sun.java.swing.plaf.windows.WindowsFileChooserUI.updateUseShellFolder(WindowsFileChooserUI.java:505)
    at com.sun.java.swing.plaf.windows.WindowsFileChooserUI.installComponents(WindowsFileChooserUI.java:196)
    at javax.swing.plaf.basic.BasicFileChooserUI.installUI(BasicFileChooserUI.java:136)
    at com.sun.java.swing.plaf.windows.WindowsFileChooserUI.installUI(WindowsFileChooserUI.java:129)
    at javax.swing.JComponent.setUI(JComponent.java:673)
    at javax.swing.JFileChooser.updateUI(JFileChooser.java:1763)
    at javax.swing.JFileChooser.setup(JFileChooser.java:360)
    at javax.swing.JFileChooser.<init>(JFileChooser.java:333)
    at javax.swing.JFileChooser.<init>(JFileChooser.java:286)
    at my.pack.Main.selectRoot(Main.java:184)
如果我从程序中的另一个点运行selectRoot(),它会正常运行

更新

伙计们,这真是一本关于编程陷阱的好书

在我的原始代码中,有一个JTextField,它保存实际路径并以这种方式初始化:

root = new JTextField();
root.setToolTipText("Root folder");
root.setText(System.clearProperty("user.dir"));
System.clearProperty()清除系统属性(为null)并返回旧值(arrrghhhh)。clearProperty()可能是从自动完成中滑入的

谢谢大家。我会给你投一些弃权票…

也许这会有帮助

new JFileChooser(System.getProperty("user.home"));
这项工作对我来说:

        import javax.swing.JDialog;
        import javax.swing.JFileChooser;
        import javax.swing.JFrame;


        public class MyDialog extends JDialog {

            public MyDialog(){
                super(new JFrame("prova"));
            }

            void selectRoot() {
                JFileChooser ch = new JFileChooser();
                ch.showOpenDialog(this);
            }


            public static void main(String[] args) throws Exception{
                MyDialog myDialog = new MyDialog();
                myDialog.show(true);
                myDialog.selectRoot();
            }

        }

确保从EDT运行showOpenDialog。如果从另一个线程调用此方法,可能会导致各种错误:

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        JFileChooser ch = new JFileChooser();
        ch.showOpenDialog((JFrame)this);
    }
});

代码中的“this”指的是什么?这是一个JFrame,它从一个JButton抓取一个ActionEvent以弹出JFleChooser。您何时调用此代码?这看起来像是初始化代码,您真的想在启动时打开文件选择器吗?确保所有GUI调用都在上进行。为了更快地得到更好的帮助,发布一条消息。我的SSCCE运行良好。。。将检查我的EDT原始代码…告诉我您是否仍然有相同的问题+1请参阅。毕竟,使用该代码它将工作,因为我正在清除user.dir:-)已接受
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        JFileChooser ch = new JFileChooser();
        ch.showOpenDialog((JFrame)this);
    }
});