getting a nullpointer exception

Reply

Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

getting a nullpointer exception

 
0
  #1
Nov 18th, 2004
I keep getting a null pointer exception with this code:
It's suppose to load a text file into a textArea.

  1. JFileChooser chooser = new JFileChooser();
  2. int returnVal = chooser.showOpenDialog(this);
  3. String getFile = chooser.getName();
  4. if(returnVal == JFileChooser.APPROVE_OPTION)
  5. {
  6. try {
  7. FileReader filegetter = new FileReader(getFile);
  8. BufferedReader br = new BufferedReader(filegetter);
  9. String line = br.readLine();
  10. while ((line)!= null)
  11. {
  12. textArea2.setText(line);
  13. }
  14. }
  15. catch (FileNotFoundException ex)
  16. {
  17. ex.printStackTrace();
  18. }
  19. catch (Exception myE)
  20. {
  21. myE.printStackTrace();
  22. }
  23. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: getting a nullpointer exception

 
0
  #2
Nov 19th, 2004
and which line throws the exception?
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: getting a nullpointer exception

 
0
  #3
Nov 19th, 2004
Originally Posted by jwenting
and which line throws the exception?
in the: public static void main(String[] args)

I have set up everything correctly just getting this one error
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: getting a nullpointer exception

 
0
  #4
Nov 19th, 2004
not which method, which line.

If you know exactly (to the line) where the exception occurrs you will probably be enlightened as to the cause.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 84
Reputation: jerbo is an unknown quantity at this point 
Solved Threads: 1
jerbo jerbo is offline Offline
Junior Poster in Training

Re: getting a nullpointer exception

 
0
  #5
Nov 19th, 2004
OK, I think I found your problem.

Change
String getFile = chooser.getName();
to
String getFile = chooser.getSelectedFile().getPath();

The reason you received a Null Pointer exception is
FileReader filegetter = new FileReader(getFile);
returns the name of the file. However getFile only contains the name of the file, not where to get it (path).

So since 'new FileReader(getFile);' can't find the file it returns NULL, hence the NPE.

Using:
getFile = chooser.getSelectedFile().getPath();
returns the fully qualified file with path information.

Hope this helps!
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: getting a nullpointer exception

 
0
  #6
Nov 19th, 2004
Thanks jerbo, that worked perfectly!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC