| | |
why is there a NullPointerException?
Thread Solved |
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:
Thank you in advance.
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)
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class DictSplitter { File dict = new File("diccionario.txt"); File easy = new File("easy.txt"); File diff = new File("difficult.txt"); File noWay = new File("xtreme.txt"); public static void main(String[] args) throws IOException { DictSplitter ds = new DictSplitter(); BufferedReader br = new BufferedReader(new FileReader(ds.dict)); BufferedWriter bwE = new BufferedWriter(new FileWriter(ds.easy)); bwE.write("Word length: up to 4 letters"); bwE.newLine(); BufferedWriter bwD = new BufferedWriter(new FileWriter(ds.diff)); bwD.write("Word length: 5 to 7 letters"); bwD.newLine(); BufferedWriter bwNW = new BufferedWriter(new FileWriter(ds.noWay)); bwNW.write("Word length: from 8 letters onwards"); String tmp = ""; int easyCount = 0, diffCount = 0, nwCount = 0; while (tmp != null) { tmp = br.readLine(); // System.out.println("tmp: " + tmp); int letters = tmp.length(); switch (letters) { case 0: case 1: case 2: case 3: case 4: bwE.write(tmp); bwE.newLine(); easyCount++; break; case 5: case 6: case 7: bwD.write(tmp); bwD.newLine(); diffCount++; break; default: bwNW.write(tmp); bwNW.newLine(); nwCount++; break; } } bwE.close(); bwD.close(); bwNW.close(); System.out.println("easy: " + easyCount + "\t\tdifficult: " + diffCount + "\t\tnoWay: " + nwCount); } }
Thank you in advance.
1
#2 33 Days Ago
'tmp' string might be null. put a check like this after you do the 'readLine' and try
java Syntax (Toggle Plain Text)
if(tmp != null) { //your code }
Last edited by Agni; 33 Days Ago at 8:07 pm.
thanks
-chandra
-chandra
•
•
Join Date: Oct 2008
Posts: 103
Reputation:
Solved Threads: 14
0
#3 33 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:
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)
while ((tmp=br.readLine()) != null){
0
#7 31 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.
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
-chandra
![]() |
Similar Threads
- NullPointerException problem T_T (Java)
- nullpointerexception error (Java)
- nullpointerexception (Java)
- error :java.lang.NullPointerException in searchuser.jsp page (JSP)
- NullPointerexception ?? (Java)
- Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException (Java)
- [B]NullPointerException[/B] (Java)
- Multi class multi form j2me app getting NullPointerException (Java)
- NullPointerException: (Java)
Other Threads in the Java Forum
- Previous Thread: How to compare string value
- Next Thread: Jbutton array
| Thread Tools | Search this Thread |
2dgraphics 3d @param affinetransform android api apple applet application arc arguments array arrays automation banking binary binarytree bluetooth chatprogramusingobjects class client code color compare component count database derby design detection eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image integer interface j2me java java.xls javadesktopapplications javaprojects jni jpanel julia keytool keyword linux list macintosh map method methods mobile netbeans object open-source os pong problem producer program project projectideas property read recursion reference replaysolutions rim scanner server set size sms sort sql stop string swing terminal threads transforms tree ui unicode validation web windows





