Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Re: NullPointerException Error?? (http://www.velocityreviews.com/forums/t124089-re-nullpointerexception-error.html)

Jon A. Cruz 07-06-2003 04:27 AM

Re: NullPointerException Error??
 
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.j ava:119)
> at java.util.StringTokenizer.<init>(StringTokenizer.j ava: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/...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.



All times are GMT. The time now is 12:28 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.