It's not a secret anymore!
The moose likes Swing / AWT / SWT and the fly likes Null pointer exception on action performed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Null pointer exception on action performed" Watch "Null pointer exception on action performed" New topic

Null pointer exception on action performed

Nimit Mohan

Joined: Jun 11, 2012
Posts: 1
The below program is used to take the input of integers from a text file and plot a XY graph.I have used JFileChooser to select the file. I'm trying to get the complete path of the file. I have created an ActionListener method for the button. When I run it I get the following message:

Exception in thread "main" java.lang.NullPointerException
at<init>(Unknown Source)
at pia.main(

How do i rectify it?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10836

Welcome to the Ranch.

In future, while posting code, please UseCodeTags I have added them this time for you. As you can see they make the code much more easier to read and understand.

The exception refers to where as you have posted Pio. Please PostRealCode for us to help you better

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

URL is still null at that time. It's not set until you've actually clicked the button and selected a file. That can be seconds, minutes, hours after you create the File object.

How To Ask Questions How To Answer Questions
Santhosh San

Joined: Sep 16, 2009
Posts: 2
Nimit :
your getting NullPointer Exceptoion , since your trying to read file outside the actionPerfomred method. in your case exception thrown here , File f = new File(URL); , here URL is always null ...
Place your file reading code inside actionPerformed method , it will work.
also use "File f = new File(file.getAbsolutePath());" to get selected file as stream..
I agree. Here's the link:
subject: Null pointer exception on action performed
It's not a secret anymore!