943,985 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4623
  • Java RSS
Nov 18th, 2004
0

getting a nullpointer exception

Expand Post »
I keep getting a null pointer exception with this code:
It's suppose to load a text file into a textArea.

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Nov 19th, 2004
0

Re: getting a nullpointer exception

and which line throws the exception?
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Nov 19th, 2004
0

Re: getting a nullpointer exception

Quote 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
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Nov 19th, 2004
0

Re: getting a nullpointer exception

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Nov 19th, 2004
0

Re: getting a nullpointer exception

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!
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004
Nov 19th, 2004
0

Re: getting a nullpointer exception

Thanks jerbo, that worked perfectly!
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Why Doesn't My Sort Work!?
Next Thread in Java Forum Timeline: problem with program finding "magic squares"





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC