In my code at a certain point i have this code
void selectRoot() {
JFileChooser ch = new JFileChooser();
ch.showOpenDialog((JFrame)this);
}
This throws that Exception:
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)
Looking in the source of java.io.Win32FileSystem.normalize() it seems the (current) path is null:
public String normalize(String path) {
int n = path.length(); // Line: 164
char slash = this.slash;
char altSlash = this.altSlash;
If i run selectRoot() from another point in the program it runs fine ...
Update
Guys that is really a good one for the great book of programing pitfalls !
In my original code there is a JTextField that holds the actual path and get initialize that way:
root = new JTextField();
root.setToolTipText("Root folder");
root.setText(System.clearProperty("user.dir"));
System.clearProperty() clears the system property (to null) and returns the old value (arrrghhhhhhh). clearProperty() probably slipped in from auto-completion.
Thank you all. Will give some up votes to you ...
Make sure to run showOpenDialog from EDT. If you call this method from another thread, it can cause all sorts of errors:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFileChooser ch = new JFileChooser();
ch.showOpenDialog((JFrame)this);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With