| | |
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 Oct 21st, 2009
'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; Oct 21st, 2009 at 8:07 pm.
thanks
-chandra
-chandra
•
•
Join Date: Oct 2008
Posts: 103
Reputation:
Solved Threads: 14
0
#3 Oct 21st, 2009
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 Oct 24th, 2009
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 |
account android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source plazmic print problem program programming project property recursion ria scanner search server set smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree webservices windows





