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
Expand|Select|Wrap|Line Numbers
- int returnVal = fc.showOpenDialog(parent)
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.But, here are the relevant bits of code:
Expand|Select|Wrap|Line Numbers
- 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:
Expand|Select|Wrap|Line Numbers
- int returnVal = fc.showOpenDialog(parent)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GUI$1.actionPerformed(GUI.java:43)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6041)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3265)
at java.awt.Component.processEvent(Component.java:580 6)
at java.awt.Container.processEvent(Container.java:205 8)
at java.awt.Component.dispatchEventImpl(Component.jav a:4413)
at java.awt.Container.dispatchEventImpl(Container.jav a:2116)
at java.awt.Component.dispatchEvent(Component.java:42 43)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3916)
at java.awt.Container.dispatchEventImpl(Container.jav a:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2440 )
at java.awt.Component.dispatchEvent(Component.java:42 43)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:121)