why is there a NullPointerException?

Thread Solved

Join Date: Aug 2008
Posts: 87
Reputation: freelancelote is an unknown quantity at this point 
Solved Threads: 2
freelancelote's Avatar
freelancelote freelancelote is offline Offline
Junior Poster in Training

why is there a NullPointerException?

 
0
  #1
Oct 21st, 2009
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 440
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training
 
1
  #2
Oct 21st, 2009
'tmp' string might be null. put a check like this after you do the 'readLine' and try

  1. if(tmp != null)
  2. {
  3. //your code
  4. }
Last edited by Agni; Oct 21st, 2009 at 8:07 pm.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 103
Reputation: cgeier is an unknown quantity at this point 
Solved Threads: 14
cgeier cgeier is offline Offline
Junior Poster
 
0
  #3
34 Days Ago
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:
  1. while ((tmp=br.readLine()) != null){
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 87
Reputation: freelancelote is an unknown quantity at this point 
Solved Threads: 2
freelancelote's Avatar
freelancelote freelancelote is offline Offline
Junior Poster in Training
 
0
  #4
34 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 440
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training
 
0
  #5
34 Days Ago
upload the data files if they are not too big, we'll see if we can help.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 87
Reputation: freelancelote is an unknown quantity at this point 
Solved Threads: 2
freelancelote's Avatar
freelancelote freelancelote is offline Offline
Junior Poster in Training
 
0
  #6
34 Days Ago
thank you,
here is the file
Attached Files
File Type: txt diccionario.txt (586.8 KB, 1 views)
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 440
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training
 
0
  #7
32 Days Ago
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.
thanks
-chandra
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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