aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Null pointer exception on action performed" Watch "Null pointer exception on action performed" New topic
Author

Null pointer exception on action performed

Nimit Mohan
Greenhorn

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 java.io.File.<init>(Unknown Source)
at pia.main(pia.java:34)

How do i rectify it?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

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 pia.java 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
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Santhosh San
Greenhorn

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: http://aspose.com/file-tools
 
subject: Null pointer exception on action performed