Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

NullPointerException in JFileChooser.showOpenDialog() JRE 1.7

843096Aug 13 2012 — edited Aug 14 2012
Hi all,

In my application, i have used JFileChooser to select a file. When i try to open JFileChooser.showOpenDialog() it throws the following exception. Not sure where i am wrong. Please help me.

Exception:

#Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
# at sun.awt.shell.Win32ShellFolder2.access$200(Unknown Source)
# at sun.awt.shell.Win32ShellFolder2$1.call(Unknown Source)
# at sun.awt.shell.Win32ShellFolder2$1.call(Unknown Source)
# at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
# at java.util.concurrent.FutureTask.run(Unknown Source)
# at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
# at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
# at sun.awt.shell.Win32ShellFolderManager2$ComInvoker$3.run(Unknown Source)
# at java.lang.Thread.run(Unknown Source)


Source Code:

public File browseFile() {
UIManager.put("FileChooser.readOnly", Boolean.TRUE);
if (DEBUG > 4) {
System.out.println("Initial Dir : " + initialDir);
System.out.println("Title : " + title);
}

JFileChooser jfc = new JFileChooser();
FileFilter filter = null;

if (null != extension && extension.trim().length() > 0) {
if (null != description && description.trim().length() > 0) {
filter = new ExtensionFilter(this.description, this.extension);
} else {
filter = new ExtensionFilter(this.extension, this.extension);
}
jfc.setFileFilter(filter);
}

if (null != title && title.trim().length() > 0) {
jfc.setDialogTitle(title);
}

if (null != initialDir) {
jfc.setCurrentDirectory(new File(initialDir));
}

int returnVal = - 1;

if (type == JFileChooser.OPEN_DIALOG) {
returnVal = jfc.showOpenDialog(this);
} else if (type == JFileChooser.SAVE_DIALOG) {
returnVal = jfc.showSaveDialog(this);
}

if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = jfc.getSelectedFile();
setVisible(false);
return file;
} else {
setVisible(false);
return null;
}

}

Thanks for all in Advance.

/dhavan
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 11 2012
Added on Aug 13 2012
7 comments
4,060 views