473,569 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JFileChooser giving NullPointerExce ption

2 New Member
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
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.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)
Jan 30 '09 #1
2 6870
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
4064
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
10725
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 chosen, turning it to grey scale, then displaying that as an Icon on another jLabel so that the user can see both of them at once. I'm pretty sure I...
13
1953
oll3i
by: oll3i | last post by:
private List<Klient> klienci; m_add_client.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { try{
3
1910
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 i think its best to let someone else to look at it now :p Here it is: import javax.swing.filechooser.*; import java.io.File;
1
2407
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 TextField t1, t2, t3;
2
2265
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 already exist) in my application using the Design Editor. I go about this by right-clicking on the JButton in the design editor, navigating to...
1
3186
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 already exist) in my application using the Design Editor. I go about this by right-clicking on the JButton in the design editor, navigating to Events...
1
17531
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 before, lending proof to Einstein's statement: “Only two things are infinite, the universe and human stupidity ...”. Main Cause Dereferencing...
3
3328
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 { ArrayList<String> dictionary; ArrayList<String> encryptedText;
0
7700
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8125
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.