Re: NullPointerException Error??

Discussion in 'Java' started by Jon A. Cruz, Jul 6, 2003.

  1. Jon A. Cruz

    Jon A. Cruz Guest

    Evan wrote:
    > I am writing a spell checkin program that I got to compile. But upon
    > execution I get a NullPointer Error.
    >
    > SpellChecker.java:
    >
    > import java.io.*;
    > import java.util.StringTokenizer;
    >
    > public class SpellChecker {
    > private BufferedReader brWordList = null;

    -------------------------------^^

    > private StringTokenizer stFileToCheck = null;

    --------------------------------^^

    Drop the Hungarian notation. With any language more strongly typed than
    C (that means, C++, Java, etc) it actually gets in the way instead of
    adding to usefullness.




    > Errors:
    >
    >
    > java.lang.NullPointerException
    > at java.util.StringTokenizer.<init>(StringTokenizer.java:119)
    > at java.util.StringTokenizer.<init>(StringTokenizer.java:135)
    > at SpellChecker.CheckSpelling(SpellChecker.java:39)


    That's simple.

    The stack trace tells you exactly where to look. So look at line 39 in
    SpellChecker.java


    > private void Dictionary(){
    > //spellCheckDict = new Dictionary();
    > try {
    > brWordList = new BufferedReader(new FileReader("WordList.txt"));
    > for(String wordToAdd = null; brWordList.readLine() != null;) {
    > wordToAdd = brWordList.readLine();
    > spellCheckDict.addWord(wordToAdd);
    > }


    First of all, that is a very bad abuse of the 'for' construct. If you
    aren't really looping on some variable, don't use 'for'

    Cleaning up the poor coding we get

    String wordToAdd = null;
    do
    {
    wordToAdd = brWordList.readLine();
    spellCheckDict.addWord(wordToAdd);
    } while ( brWordList.readLine() != null );

    Hmmm.... doing so exposes a probable bug.

    Do you really mean to read a value, skip a value, read a value, skip
    a value?

    Did you maybe need this instead?

    String wordToAdd = null;
    while ( (wordToAdd = brWordList.readLine()) != null )
    {
    spellCheckDict.addWord(wordToAdd);
    }


    >
    > }
    > catch (IOException e){
    > //finish this later
    > }


    Oooohhhh. Very bad. Especially with code that's not finished and
    production, *never* have an empty catch. At the least do
    e.printStackTrace() there.





    > for(String wordToCheck = null; brFileToCheck != null;) {


    Again, another scary looking abuse of the for loop. We'll skip that for now.

    > wordToCheck = brFileToCheck.readLine();
    > stFileToCheck = new StringTokenizer(wordToCheck, "-'.,!?\"");


    Was that just line 39 we passed? Probably. Hmmm... now what ever could
    be causing a null pointer exception there?

    Well... there's only one variable involved: 'wordToCheck'. So....
    Hmmmm... how ever could that have become null???

    I know! Let's look at the code. Hmmm.... it's the result of a readLine()
    call. Let's look at the documentation, shall we?

    http://java.sun.com/j2se/1.4.1/docs/api/java/io/BufferedReader.html#readLine()

    > 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


    Eeeek! It returns null, and you use it unchecked. Aha! That's probably it.


    > String wordProcessed;
    >
    > if(stFileToCheck.hasMoreTokens()) {
    > wordLocation++;
    > wordProcessed = stFileToCheck.nextToken();
    >
    > if(spellCheckDict.containsWord(wordProcessed) == false) {
    > result = wordProcessed + " At Word:" + wordLocation + "/n";
    > }
    > }
    > }



    > }
    > catch(IOException e){
    > }


    Again, you should try to never have an empty catch statement.
     
    Jon A. Cruz, Jul 6, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tohru Kao
    Replies:
    3
    Views:
    446
    Neil Masson
    Jul 14, 2003
  2. Tohru Kao
    Replies:
    1
    Views:
    405
    Chris
    Jul 8, 2003
  3. Dhek Bhun Kho
    Replies:
    0
    Views:
    2,245
    Dhek Bhun Kho
    Jul 9, 2003
  4. Tom
    Replies:
    12
    Views:
    8,150
    Chris Smith
    Aug 5, 2003
  5. luch
    Replies:
    3
    Views:
    522
    Roedy Green
    Sep 25, 2005
Loading...

Share This Page