• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null pointer exception on action performed

 
Nimit Mohan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11164
14
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Santhosh San
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic