I am trying to learn how to use the JFileChooser. I'm working in BlueJ. I keep getting a NullPointerExce ption. Full text of the error message is at the bottom of the post.
My code is based on some of the examples I've seen online - in fact, it is almost identical. The only things I've changed (I think) are variable names. The main one I've been using for reference is from Sun's Java Tutorials.
Admittedly, I don't fully understand the whole
thing... The way it's described (in the Sun Tutorial) seems to say that it's really only there for on-screen placement? (At least, that's how I interpretted it):
But, here are the relevant bits of code:
In case I didn't mention this already, I'm just new to Java GUI programming. Pretty much everything I'm doing is drawing off examples I've been given or found online.
Here's the error: (GUI:java:43 refers to what is line 43 in my editor. I've left out a few lines that aren't relevant to the error. But it's this line:
)
My code is based on some of the examples I've seen online - in fact, it is almost identical. The only things I've changed (I think) are variable names. The main one I've been using for reference is from Sun's Java Tutorials.
Admittedly, I don't fully understand the whole
Code:
int returnVal = fc.showOpenDialog([I]parent[/I])
The argument to the showOpenDialog method specifies the parent component for the dialog. The parent component affects the position of the dialog and the frame that the dialog depends on. For example, the Java look and feel places the dialog directly over the parent component. If the parent component is in a frame, then the dialog is dependent on that frame. This dialog disappears when the frame is minimized and reappears when the frame is maximized.
Code:
public class GUI extends JFrame {
//instance variables
private JTextField filenameTF; //A textbox to display the current file
private JButton browse; //Browse button
JFileChooser fc;
File gbFile; //The file I need to work with in my program
public GUI() {
// Get content pane of frame
Container pane = getContentPane();
//create and add GUI components to pane
filenameTF = new JTextField("Select File");
filenameTF.setEditable(false);
pane.add(filenameTF, BorderLayout.LINE_START);
browse = new JButton("Browse...");
pane.add(browse, BorderLayout.LINE_END);
//Define, create and register an action lister
browse.addActionListener(new ActionListener () {
public void actionPerformed (ActionEvent e) {
int returnVal = fc.showOpenDialog(GUI.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
gbFile = fc.getSelectedFile();
filenameTF.setText(gbFile.getName());
}
}});
}
public static void main(String[] args)
{
JFrame f = new GUI();
f.pack(); //may need to change?
f.setVisible(true);
//Define, create and register a listener to quit the application
//when the window is closed.
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}});
}
}
Here's the error: (GUI:java:43 refers to what is line 43 in my editor. I've left out a few lines that aren't relevant to the error. But it's this line:
Code:
int returnVal = fc.showOpenDialog([I]parent[/I])
Exception in thread "AWT-EventQueue-0" java.lang.NullP ointerException
at GUI$1.actionPer formed(GUI.java :43)
at javax.swing.Abs tractButton.fir eActionPerforme d(AbstractButto n.java:1995)
at javax.swing.Abs tractButton$Han dler.actionPerf ormed(AbstractB utton.java:2318 )
at javax.swing.Def aultButtonModel .fireActionPerf ormed(DefaultBu ttonModel.java: 387)
at javax.swing.Def aultButtonModel .setPressed(Def aultButtonModel .java:242)
at javax.swing.pla f.basic.BasicBu ttonListener.mo useReleased(Bas icButtonListene r.java:236)
at java.awt.Compon ent.processMous eEvent(Componen t.java:6041)
at javax.swing.JCo mponent.process MouseEvent(JCom ponent.java:326 5)
at java.awt.Compon ent.processEven t(Component.jav a:5806)
at java.awt.Contai ner.processEven t(Container.jav a:2058)
at java.awt.Compon ent.dispatchEve ntImpl(Componen t.java:4413)
at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:2116)
at java.awt.Compon ent.dispatchEve nt(Component.ja va:4243)
at java.awt.Lightw eightDispatcher .retargetMouseE vent(Container. java:4322)
at java.awt.Lightw eightDispatcher .processMouseEv ent(Container.j ava:3986)
at java.awt.Lightw eightDispatcher .dispatchEvent( Container.java: 3916)
at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:2102)
at java.awt.Window .dispatchEventI mpl(Window.java :2440)
at java.awt.Compon ent.dispatchEve nt(Component.ja va:4243)
at java.awt.EventQ ueue.dispatchEv ent(EventQueue. java:599)
at java.awt.EventD ispatchThread.p umpOneEventForF ilters(EventDis patchThread.jav a:273)
at java.awt.EventD ispatchThread.p umpEventsForFil ter(EventDispat chThread.java:1 83)
at java.awt.EventD ispatchThread.p umpEventsForHie rarchy(EventDis patchThread.jav a:173)
at java.awt.EventD ispatchThread.p umpEvents(Event DispatchThread. java:168)
at java.awt.EventD ispatchThread.p umpEvents(Event DispatchThread. java:160)
at java.awt.EventD ispatchThread.r un(EventDispatc hThread.java:12 1)
at GUI$1.actionPer formed(GUI.java :43)
at javax.swing.Abs tractButton.fir eActionPerforme d(AbstractButto n.java:1995)
at javax.swing.Abs tractButton$Han dler.actionPerf ormed(AbstractB utton.java:2318 )
at javax.swing.Def aultButtonModel .fireActionPerf ormed(DefaultBu ttonModel.java: 387)
at javax.swing.Def aultButtonModel .setPressed(Def aultButtonModel .java:242)
at javax.swing.pla f.basic.BasicBu ttonListener.mo useReleased(Bas icButtonListene r.java:236)
at java.awt.Compon ent.processMous eEvent(Componen t.java:6041)
at javax.swing.JCo mponent.process MouseEvent(JCom ponent.java:326 5)
at java.awt.Compon ent.processEven t(Component.jav a:5806)
at java.awt.Contai ner.processEven t(Container.jav a:2058)
at java.awt.Compon ent.dispatchEve ntImpl(Componen t.java:4413)
at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:2116)
at java.awt.Compon ent.dispatchEve nt(Component.ja va:4243)
at java.awt.Lightw eightDispatcher .retargetMouseE vent(Container. java:4322)
at java.awt.Lightw eightDispatcher .processMouseEv ent(Container.j ava:3986)
at java.awt.Lightw eightDispatcher .dispatchEvent( Container.java: 3916)
at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:2102)
at java.awt.Window .dispatchEventI mpl(Window.java :2440)
at java.awt.Compon ent.dispatchEve nt(Component.ja va:4243)
at java.awt.EventQ ueue.dispatchEv ent(EventQueue. java:599)
at java.awt.EventD ispatchThread.p umpOneEventForF ilters(EventDis patchThread.jav a:273)
at java.awt.EventD ispatchThread.p umpEventsForFil ter(EventDispat chThread.java:1 83)
at java.awt.EventD ispatchThread.p umpEventsForHie rarchy(EventDis patchThread.jav a:173)
at java.awt.EventD ispatchThread.p umpEvents(Event DispatchThread. java:168)
at java.awt.EventD ispatchThread.p umpEvents(Event DispatchThread. java:160)
at java.awt.EventD ispatchThread.r un(EventDispatc hThread.java:12 1)
Comment