• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help with NullPointerException

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get this error at the end of my code execution. Could anyone tell me why and how I fix it.
<message>
java.lang.NullPointerException
at java.util.StringTokenizer.<init>(StringTokenizer.java:122)
at java.util.StringTokenizer.<init>(StringTokenizer.java:138)
at App3.<init>(App3.java:20)
at App3.main(App3.java:42)
</message>
<code (App3.java)>
import java.io.*;
import java.util.*;
public class App3 {
public App3 ( ) throws IOException, NullPointerException {
BufferedReader br = new BufferedReader(new FileReader(new File("data.txt")));
BufferedWriter out = new BufferedWriter(new FileWriter(new File("finalTest.txt")));
String c="| |";
int temp;
StringTokenizer st;
String line;

//while((line = br.readLine()) != null){
while(true){

//if( (line=br.readLine()) == null) break;
st = new StringTokenizer(br.readLine(),c);
temp = st.countTokens();

System.out.println("The Number of tokens found: " + temp);

for(int i = 0; i < temp; i++){

if(st.countTokens()!=0){
System.out.println(" " + st.nextElement());
//out.write(" " + st.nextElement());
} else{
System.out.println(" ");
//out.write(" " + st.nextToken());
} // end if
} //for

} //end while

} //end of constructor
public static void main ( String args[] ) throws IOException, NullPointerException {
new App3();
}//End of Main Method
} // End of Application
</code>
<My Text file contains (data.txt)>
PM1|null|null|11|XX|PID1|HOT1|PLAZA1|NO. 41|Main Description1|
PM2|null|null|12|XX|PID2|HOT2|PLAZA2|NO. 42|Main Description2|
PM3|null|null|13|XX|PID3|HOT3|PLAZA3|NO. 43|Main Description3|
</My Text file contains (data.txt)>
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NullPointerException's are thrown when operation is performed on a null value.
You should not throw it. You have to prevent it from happening.
One way is check if a value is null before doing anything with it.
For eg: if you have a null string. If you do a string.length(), this will throw a nullpointerexception.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

Instead of using " while(true)" why don't u try some more logical comparision like while(st.hasMoreTokens()) .....I hope that will help u out ....

regards
reply
    Bookmark Topic Watch Topic
  • New Topic