944,106 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 916
  • Java RSS
Oct 21st, 2009
0

why is there a NullPointerException?

Expand Post »
I'd appreciate if somebody could have a look at this code and tell me where the problem is.
The program is meant to take a .txt file and split it in three different files: one with words of 1 to 4 letters, one with words of 5 to 7 and the last one with words of 8 letters onwards.
For some reason the program starts and does well (that is, behaves as expected) until it reaches about the middle of the original file. Then I get a NullPointerException: line 34.
Two of the destination files are OK and one is truncated.
Here is the code:
java Syntax (Toggle Plain Text)
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8.  
  9.  
  10. public class DictSplitter {
  11. File dict = new File("diccionario.txt");
  12. File easy = new File("easy.txt");
  13. File diff = new File("difficult.txt");
  14. File noWay = new File("xtreme.txt");
  15.  
  16. public static void main(String[] args) throws IOException {
  17. DictSplitter ds = new DictSplitter();
  18.  
  19. BufferedReader br = new BufferedReader(new FileReader(ds.dict));
  20. BufferedWriter bwE = new BufferedWriter(new FileWriter(ds.easy));
  21. bwE.write("Word length: up to 4 letters");
  22. bwE.newLine();
  23. BufferedWriter bwD = new BufferedWriter(new FileWriter(ds.diff));
  24. bwD.write("Word length: 5 to 7 letters");
  25. bwD.newLine();
  26. BufferedWriter bwNW = new BufferedWriter(new FileWriter(ds.noWay));
  27. bwNW.write("Word length: from 8 letters onwards");
  28. String tmp = "";
  29. int easyCount = 0, diffCount = 0, nwCount = 0;
  30.  
  31. while (tmp != null) {
  32. tmp = br.readLine();
  33. // System.out.println("tmp: " + tmp);
  34. int letters = tmp.length();
  35. switch (letters) {
  36. case 0:
  37. case 1:
  38. case 2:
  39. case 3:
  40. case 4:
  41. bwE.write(tmp);
  42. bwE.newLine();
  43. easyCount++;
  44. break;
  45. case 5:
  46. case 6:
  47. case 7:
  48. bwD.write(tmp);
  49. bwD.newLine();
  50. diffCount++;
  51. break;
  52. default:
  53. bwNW.write(tmp);
  54. bwNW.newLine();
  55. nwCount++;
  56. break;
  57. }
  58. }
  59. bwE.close();
  60. bwD.close();
  61. bwNW.close();
  62. System.out.println("easy: " + easyCount + "\t\tdifficult: " +
  63. diffCount + "\t\tnoWay: " + nwCount);
  64. }
  65. }

Thank you in advance.
Similar Threads
Reputation Points: 34
Solved Threads: 2
Junior Poster in Training
freelancelote is offline Offline
88 posts
since Aug 2008
Oct 21st, 2009
1
Re: why is there a NullPointerException?
'tmp' string might be null. put a check like this after you do the 'readLine' and try

java Syntax (Toggle Plain Text)
  1. if(tmp != null)
  2. {
  3. //your code
  4. }
Last edited by Agni; Oct 21st, 2009 at 8:07 pm.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Oct 21st, 2009
0
Re: why is there a NullPointerException?
The documentation for BufferedReader states the following ..."Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached." When you reach the end of the file, the value is "null".

http://java.sun.com/j2se/1.5.0/docs/...tml#readLine()

As Agni said, tmp is null which is why you are getting the NullPointerException. It happens because you try to do the following when tmp = null:

int letters = tmp.length();

You can use an "if" statement as Agni mentioned or just combine lines 31 and 32 as follows:
Java Syntax (Toggle Plain Text)
  1. while ((tmp=br.readLine()) != null){
Reputation Points: 41
Solved Threads: 15
Junior Poster
cgeier is offline Offline
104 posts
since Oct 2008
Oct 22nd, 2009
0
Re: why is there a NullPointerException?
Hi, thanks to both. That solved the problem although I still don't quite understand.
Cgeier, the thing is, the NullPointerException doesn't come when it's finished reading the original file. It comes in the middle of the file and then continues reading until the end.
Reputation Points: 34
Solved Threads: 2
Junior Poster in Training
freelancelote is offline Offline
88 posts
since Aug 2008
Oct 22nd, 2009
0
Re: why is there a NullPointerException?
upload the data files if they are not too big, we'll see if we can help.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Oct 22nd, 2009
0
Re: why is there a NullPointerException?
thank you,
here is the file
Attached Files
File Type: txt diccionario.txt (586.8 KB, 343 views)
Reputation Points: 34
Solved Threads: 2
Junior Poster in Training
freelancelote is offline Offline
88 posts
since Aug 2008
Oct 24th, 2009
0
Re: why is there a NullPointerException?
The null is because it's reading past the last line of the file. So when it has read the last line the 'while' is true, then it reads the next line(one after last one) then when you access the length of tmp you get a nullpointer exception. re-write your loop to prevent that.

As a future strategy, try to put a lot of prints and you might be able to figure out such bugs very quickly.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: How to compare string value
Next Thread in Java Forum Timeline: Calculation Formula... Anyone can Help?





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


Follow us on Twitter


© 2011 DaniWeb® LLC