• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting exception with buffered loop...  RSS feed

 
Jonathan King
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all... I have a loop to read a line of data in, then get tokens from the line and perform actions on those tokens. However, I'm not sure my reading in of data loop is correct, because after the processing of the data, I still get some errors. Specifically:
"Exception in thread "main" java.lang.NullPointerException
at java.util.StringTokenizer.<init>(Unknown Source)
at java.util.StringTokenizer.<init>(Unknown Source)
at Card.read(Card.java:41)"
here is the code that I have for this loop, if anyone can help me to tweak it, that would be great!!

Thanks ahead of time...
Jon
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the stack trace for references to classes under your control:

What exactly is on line 41 of Card.java? We can't identify your line numbers from here, so this is important info for you to provide. But it looks like the error is in a StringTokenizer constructor, and since that only occurs in one place, I guess it must be this line:

It's hard to see anything wrong with this, since you just checked for null. Try inserting a print statement just before this, so we can see what exactly is in the line that was read which caused the NullPointerException.
 
Jonathan King
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm... Line 41 in Card was this:

a println statement of what inputLine is, does not print anything if I place it right after that line.
umm...
this class is part of a larger class... do you think the error comes from somewhere in an earlier calling?
I didn't think it was appropriate to post the WHOLE program code that I had for this program. But is claioms to have the same null pointer error in all of my classes. at java.util.StringTokenizer. But when I have the print of cardRank and cardSuit, it prints out the proper output, so it seems to me it reads the line in, and processes each token properly.
Thanks!
Jon
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[B]Hmm... Line 41 in Card was this:
[/B]
Well there appears to be no way that line could possibly call a StringTokenizer constructor, or throw a NullPointerException. Which probably means that your program is executing using a Card.class file which does not correspond to the latest version of Card.java. Try deleting Card.class - heck, delete all .class files - then recompile. If that doesn't fix your problem, study your class path carefully. Most likely, somewhere on the class path you have some old class files you've forgotten about.
 
Jonathan King
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
Thanks for the help. I didn't realize that I had to delete the class files. I thought recompiling would do that automatically. I went ahead and deleted, and recompiled, and now it appears to work like a charm! Thanks for the help.
Jon
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, javac unfortunately creates the illusion it's smarter than it really is. When there are no class files, it compiles them as necessary from the java files it finds. Afterwards though, if it find a class file it just uses it, without checking to see if maybe there's a new java file to compile. Unless your javac command specifically told it to compile that file. Modern IDEs will generally take care of this for you, but if you're not using one, it's helpful to have some sort of process that guarantees you don't have out-of-date class files lying around. I used to make a small script/batch file for compilation, e.g.
rmdir classes
mkdir classes
javac -sourcepath src -d classes pkg/subpakg/MyClass.java
This assumes all source code is under a directory "src", and puts all classes in another dir "classes", completely regenerated each time. There's a lot more you can do with this, but nowadays you'd probably want to do this with Ant instead, so I won't go into further detail. The point is, one way or another it's worth your time to make sure you're not dealing with out-of date class files. Deleting all the old class files in a script is one simple way to do it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!