-
AWT Desktop Open
I'm attempting to open a pdf help file on button click. However I get a null pointer exception on button click. I'm not sure where this would be coming from, unless somehow the Desktop is returning null. I haven't used the AWT Desktop before, so I'm not familiar with how it works very well.
None of the JOptionPanes show, leading me to below it isn't even catching the error. Error line is the Desktop.getDesktop() line. Both the listener and the error code are below. The package structure leading to the file I'm accessing is si/documentation/HELP STUFF.pdf. Thanks in advance!
Code:
/**
* Opens help documentation
*
* @author stnorris
*/
class helpDocListener implements ActionListener{
private GUI gui;
public helpDocListener(GUI gui){
this.gui = gui;
}
@Override
public void actionPerformed(ActionEvent arg0) {
try {
if(Desktop.isDesktopSupported()){
Desktop.getDesktop().open(new File(ClassLoader.getSystemResource("si/documentation/HELP STUFF.pdf").getFile()));
}
else{
JOptionPane.showMessageDialog(gui, "Could not load documentation.", "Error", JOptionPane.ERROR_MESSAGE);
}
} catch (IOException e) {
JOptionPane.showMessageDialog(gui, "Could not load documentation.", "Error", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
}
STACK TRACE
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at si.gui.GUI$1helpDocListener.actionPerformed(GUI.ja va:1026)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unk nown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mou seReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
-
Re: AWT Desktop Open
Cross posted at AWT Desktop Open
-
Re: AWT Desktop Open
Solved it. I was so used to seeing special character code I didn't notice it at first. The ClassLoader.getSystemResource(path).getFile() was replacing all spaces with %20. I'm assuming this is a URL property I didn't know about. So running filename = filename.replace("%20"," ") fixed the path, and thus found the file.