473,467 Members | 1,477 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

JFileChooser giving NullPointerException

2 New Member
I am trying to learn how to use the JFileChooser. I'm working in BlueJ. I keep getting a NullPointerException. 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
Expand|Select|Wrap|Line Numbers
  1. int returnVal = fc.showOpenDialog(parent)
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):
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
  1. public class GUI extends JFrame {
  2.  
  3.     //instance variables
  4.     private JTextField filenameTF; //A textbox to display the current file
  5.     private JButton browse; //Browse button
  6.     JFileChooser fc;
  7.     File gbFile; //The file I need to work with in my program
  8.  
  9.     public GUI() {
  10.         // Get content pane of frame
  11.         Container pane = getContentPane();
  12.         //create and add GUI components to pane
  13.  
  14.         filenameTF = new JTextField("Select File");
  15.         filenameTF.setEditable(false);
  16.         pane.add(filenameTF, BorderLayout.LINE_START);
  17.  
  18.         browse = new JButton("Browse...");
  19.         pane.add(browse, BorderLayout.LINE_END);
  20.         //Define, create and register an action lister
  21.         browse.addActionListener(new ActionListener () {
  22.             public void actionPerformed (ActionEvent e) {
  23.                 int returnVal = fc.showOpenDialog(GUI.this);
  24.                 if (returnVal == JFileChooser.APPROVE_OPTION) {
  25.                     gbFile = fc.getSelectedFile();
  26.                     filenameTF.setText(gbFile.getName());
  27.                 }
  28.             }});
  29.     }
  30.  
  31.     public static void main(String[] args)
  32.     {
  33.         JFrame f = new GUI();
  34.         f.pack(); //may need to change?
  35.         f.setVisible(true);
  36.  
  37.         //Define, create and register a listener to quit the application
  38.         //when the window is closed.
  39.         f.addWindowListener(new WindowAdapter() {
  40.             public void windowClosing(WindowEvent e) {
  41.                 System.exit(0);
  42.             }});
  43.  
  44.     }
  45. }
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:
Expand|Select|Wrap|Line Numbers
  1. 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)
Jan 30 '09 #1
2 6850
JosAH
11,448 Recognized Expert MVP
Nowhere in the source code do you actually create a JFileChooser; your member variable fc will have its initial value which is null. Read the API documentation of the JFileChooser class and see how you can create one.

kind regards,

Jos
Jan 30 '09 #2
dragonridingsorceress
2 New Member
Ah, I found the problem. I hadn't actually initialised the FileChooser.
Jan 30 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: gabryh | last post by:
Hi, The following code throws me NullPointerException. ..... public static boolean isEmpty(String value) { return ((value == null) || (value.trim().equals(""))); }
3
by: Koji | last post by:
Basically, I have a GUI class where you have to use the JFileChooser class to select a jpg, bmp, or gif file. Then, that file is displayed on a jLabel as an Icon. The next step is taking that file...
13
oll3i
by: oll3i | last post by:
private List<Klient> klienci; m_add_client.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { ...
3
by: FinPingvin | last post by:
Hi! This code wont work properly, it returns true for showing all maps, but not any files. It is supposed to filter the JFileChooser to only show *.mp3 files. I have looked at it too long now, so...
1
by: ketand1 | last post by:
import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import java.sql.*; import java.lang.*; class DbAwt extends Frame implements ActionListener { private...
2
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
1
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
1
by: r035198x | last post by:
This exception occurs often enough in practice to warrant its own article. It is a very silly exception to get because it's one of the easiest exceptions to avoid in programming. Yet we've all got it...
3
by: chris123456789 | last post by:
Hi, when I run my code I get a NullPointerException:null. Here is the part of the code where the error occurs: import java.util.*; import java.io.*; public class Decrypt { ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.