NullPointerException in JFileChooser

Hello,

I have a method which shows a JFileChooser to select a save name. After click on the dialog's OK button, _sometimes_ it throws NullPointerException.

I have checked the thread, and everything runs in EDT, so this should not be the problem.

The calling code is

public File getSelectedFile() {

chooser = new JFileChooser(prefs.get(Configuration.KEY_LAST_SAVED_DIR, null));

chooser.setSelectedFile(new File(defaultFileName));

for(ExtensionFileFilter f : fileTypes){

chooser.addChoosableFileFilter(f);

}

if (defaultFilter == null) {

chooser.setFileFilter(chooser.getAcceptAllFileFilter());

} else {

chooser.setFileFilter(defaultFilter);

}

// That's where it happens

int returnVal = chooser.showSaveDialog(parentComponent);

//

prefs.put(Configuration.KEY_LAST_SAVED_DIR,chooser.getCurrentDirectory().getAbsolutePath());

return chooser.getSelectedFile();

}

--

Can you help me spot where the problem is?

The stack trace is below.

Thanks for any suggestions.

Karl

Exception occurred during event dispatching:

java.lang.NullPointerException

at javax.swing.JComponent.repaint(JComponent.java:4728)

at sun.swing.FilePane$2.repaintListSelection(FilePane.java:114)

at sun.swing.FilePane$2.repaintSelection(FilePane.java:104)

at sun.swing.FilePane$2.focusLost(FilePane.java:99)

at java.awt.AWTEventMulticaster.focusLost(AWTEventMulticaster.java:213)

at java.awt.Component.processFocusEvent(Component.java:5930)

at java.awt.Component.processEvent(Component.java:5794)

at java.awt.Container.processEvent(Container.java:2058)

at java.awt.Component.dispatchEventImpl(Component.java:4410)

at java.awt.Container.dispatchEventImpl(Container.java:2116)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)

at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:878)

at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:551)

at java.awt.Component.dispatchEventImpl(Component.java:4282)

at java.awt.Container.dispatchEventImpl(Container.java:2116)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:177)

at java.awt.Dialog$1.run(Dialog.java:1039)

at java.awt.Dialog$3.run(Dialog.java:1091)

at java.security.AccessController.doPrivileged(Native Method)

at java.awt.Dialog.show(Dialog.java:1089)

at javax.swing.JFileChooser.showDialog(JFileChooser.java:723)

at javax.swing.JFileChooser.showSaveDialog(JFileChooser.java:651)

at io.FileSaver.getSelectedFile(FileSaver.java:141)

at io.SaverManager.<init>(SaverManager.java:96)

at io.SaverHolder$SaveSelector.actionPerformed(SaverHolder.java:153)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)

at java.awt.Component.processMouseEvent(Component.java:6038)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)

at java.awt.Component.processEvent(Component.java:5803)

at java.awt.Container.processEvent(Container.java:2058)

at java.awt.Component.dispatchEventImpl(Component.java:4410)

at java.awt.Container.dispatchEventImpl(Container.java:2116)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)

at java.awt.Container.dispatchEventImpl(Container.java:2102)

at java.awt.Window.dispatchEventImpl(Window.java:2429)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

[5185 byte] By [kitschena] at [2007-11-27 3:50:08]
# 1

> The calling code is

>

> public File getSelectedFile() {

> chooser = new

> JFileChooser(prefs.get(Configuration.KEY_LAST_SAVED_D

> R, null));

> ...

Maybe this is it? You initialize the file chooser with a null current directory in case there is no such saved in the preferences.

Try giving some other default (such as the "java.home" system property, for example) and see if it crashes again.

Greets,

Mike

MikePa at 2007-7-12 8:54:02 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for your answers

I will try that, but I'm not very optimistic about it because:

1. javadoc says for this constructor that "(...)Passing in a null file causes the file chooser to point to the user's default directory. (...)"

2. I know that the key exists, and it does sometimes work (like two times in a row, without restarting the app or doing anything else inbetween)

Any other ideas?

Karl

kitschena at 2007-7-12 8:54:02 > top of Java-index,Desktop,Core GUI APIs...
# 3

Well then I'd launch the code in the debugger (which would normally pause when an exception is thrown) and see the exact source line which causes the exception. This requires you have the java API source code, though. Then with a bit luck you could track back where that particular null value comes from...

MikePa at 2007-7-12 8:54:02 > top of Java-index,Desktop,Core GUI APIs...
# 4

My first answer was wrong because the constructor which can handle a null input is the one taking a File. But from the prefs I get a String... So the I need to check first if the string is null and make a File out of it...

About your idea to use the debugger, I can see where the problem happens (see also the stack trace above). It's when the dialog is showing (Dialog.show has been called) and then, after pressing OK, the Component gets repainted

java.lang.NullPointerException

at javax.swing.JComponent.repaint(JComponent.java:4728)

Unfortunately the problem is only showing once in a while, so it's hard to test solutions...

Thanks anyway

Karl

kitschena at 2007-7-12 8:54:02 > top of Java-index,Desktop,Core GUI APIs...
# 5

The line where the exception is thrown is only rarely the line where the actual bug lies. It is very unlikely that the JComponent itself has a bug causing the NullPointerException. Instead, it probably throws the exception because it has received a null value from outside, probably indirectly from your code. Thus, if you can't find the error in your code, you could start from the JComponent method, see where the null value comes from and track it along the call stack back to your code.

This is surely a last resort since it can prove very tedious...

MikePa at 2007-7-12 8:54:02 > top of Java-index,Desktop,Core GUI APIs...
# 6
This is actually a known a very reproducible bug. I'm getting it too. Check out http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6561072This should be easy for sun to fix. Please vote for it...
andrewbrancha at 2007-7-12 8:54:02 > top of Java-index,Desktop,Core GUI APIs...
# 7

You could try something like this: -

JFileChooser jc = new JFileChooser();

if (fc.showDialog(this, "Title") == JFileChooser.APPROVE_OPTION) {

File file = fc.getSelectedFile();

// Rest of your code here

}

I have never experienced any null pointer exceptions doing it this way

c0demonk3ya at 2007-7-12 8:54:02 > top of Java-index,Desktop,Core GUI APIs...