2011-07-05 67 views
3

在我的代碼的JFileChooser在某一點上我有這樣的代碼失敗

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) 

展望java.io.Win32FileSystem.normalize源()它看來,(目前)路徑爲空:

public String normalize(String path) { 
     int n = path.length(); // Line: 164 
     char slash = this.slash; 
     char altSlash = this.altSlash; 

如果我運行從程序中的另一點selectRoot()運行良好...

更新

傢伙真的是一個很好的一個編程缺陷的偉大的書!

我在原來的代碼中有保存實際路徑,並得到初始化這樣一個JTextField:

root = new JTextField(); 
root.setToolTipText("Root folder"); 
root.setText(System.clearProperty("user.dir")); 

System.clearProperty()清除系統屬性(爲null)返回舊值(arrrghhhhhhh)。 clearProperty()可能會從自動完成中滑入。

謝謝大家。將給予一定的選票給你...

+0

代碼引用中的「this」是什麼? – jzd

+0

這是一個JFrame,它從JButton中抓取一個ActionEvent來彈出JFleChooser。 – PeterMmm

+0

你什麼時候調用這個代碼?它看起來像初始化代碼,你真的想在啓動時打開文件選擇器嗎? – Thomas

回答

1

也許這將幫助

new JFileChooser(System.getProperty("user.home")); 
+0

畢竟與該代碼,它將工作,因爲我清除user.dir :-)接受 – PeterMmm

1

這項工作對我說:

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

     } 
+0

告訴我,如果你仍然有同樣的問題 – Shilaghae

3

確保從美國東部時間運行showOpenDialog。如果您從另一個線程調用此方法,則可能導致各種錯誤:

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

+1另請參見[初始線程](http://download.oracle.com/ JavaSE的/教程/ uiswing /併發/ initial.html)。 – trashgod

相關問題