Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

NullPointerException Error??

2 views
Skip to first unread message

Evan

unread,
Jul 5, 2003, 9:16:56 PM7/5/03
to
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;
private BufferedReader brFileToCheck = null;
private Dictionary spellCheckDict = null;
private String result;
private int wordLocation;

public SpellChecker(){

}

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);
}

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

}

public String CheckSpelling(String fileName) {
spellCheckDict = new Dictionary();
try {
brFileToCheck = new BufferedReader(new FileReader(fileName));

for(String wordToCheck = null; brFileToCheck != null;) {
wordToCheck = brFileToCheck.readLine();
stFileToCheck = new StringTokenizer(wordToCheck, "-'.,!?\"");
String wordProcessed;

if(stFileToCheck.hasMoreTokens()) {
wordLocation++;
wordProcessed = stFileToCheck.nextToken();

if(spellCheckDict.containsWord(wordProcessed) == false) {
result = wordProcessed + " At Word:" + wordLocation + "/n";
}
}
}
}
catch(IOException e){
}
return result;
}

}

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)
at JavaSpellCheckerGUI.actionPerformed(JavaSpellCheckerGUI.java:64)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:17
67)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Abstra
ctButton.java:1820)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:419)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:257
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:258)
at java.awt.Component.processMouseEvent(Component.java:5021)
at java.awt.Component.processEvent(Component.java:4818)
at java.awt.Container.processEvent(Container.java:1525)
at java.awt.Component.dispatchEventImpl(Component.java:3526)
at java.awt.Container.dispatchEventImpl(Container.java:1582)
at java.awt.Component.dispatchEvent(Component.java:3367)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3359
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3074)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3004)
at java.awt.Container.dispatchEventImpl(Container.java:1568)
at java.awt.Window.dispatchEventImpl(Window.java:1581)
at java.awt.Component.dispatchEvent(Component.java:3367)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:445)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:191)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:144)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:130)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:98)
Anyone have any ideas. If you want to seem my GUI code Illpost it.

Jon A. Cruz

unread,
Jul 6, 2003, 12:27:07 AM7/6/03
to
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

unread,
Jul 6, 2003, 1:10:36 AM7/6/03
to
Jon A. Cruz wrote:

> Evan wrote:
>
>
>> for(String wordToCheck = null; brFileToCheck != null;) {
>
>
> Again, another scary looking abuse of the for loop. We'll skip that for
> now.
>

Ok. Back. Let's take a look at that loop.


>> try {
>> brFileToCheck = new BufferedReader(new FileReader(fileName));
>>

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

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

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


Ok. Bad news. If you did put in a check for null before using
wordToCheck, your 'for' loop would have gone into infinite loop, as the
buffered reader would never just become null.


String message = "{0} At Word:{1}\n";

brFileToCheck = new BufferedReader(new FileReader(fileName));

String wordToCheck = null;

while ( (wordToCheck = brFileToCheck.readLine) != null)
{


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

String wordProcessed;

if(stFileToCheck.hasMoreTokens()) {
wordLocation++;
wordProcessed = stFileToCheck.nextToken();

if( !spellCheckDict.containsWord(wordProcessed) ) {
result = MessageFormat.format( message, new
Object[]{wordProcessed, wordLocation} );
}
}
}


And declare the function to throw IOException. Hmm... it might even be
good to make the function take java.io.File instead of just a String.


Jon A. Cruz

unread,
Jul 6, 2003, 1:28:34 AM7/6/03
to
Evan wrote:


> private void Dictionary(){

Oops, I fogot to mention a few things on naming.

In general Java naming convention, classes and constructors start
uppercase, while variables and method names start lowercase.

Thus you would have this instead:

private void dictionary().


Now... for general OO naming. It's best that class names be nouns, and
method names be verbs. Describe what it is the method does.

So...

that would then change to something like:

private void loadDictionary()
or
private void initDictionary()

and this:

> public String CheckSpelling(String fileName) {

would go to
public String checkSpelling(String fileName)

However... it's not so clear what that method does.


It might better be changed to
public String checkSpellingAndThenReturnOnlyTheLastResultFound(File
file)


;-)

0 new messages