• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Null pointer exception on action performed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic