This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes exception in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "exception in thread "main" error" Watch "exception in thread "main" error" New topic
Author

exception in thread "main" error

Tom Keegan
Greenhorn

Joined: Dec 30, 2010
Posts: 3
Hi all, very new to java, my first week. Im going through "Beginning Programming with Java for Dummies" and trying to get one of the program examples to work but keep getting an error

the code from the book is


it compiles fine, but when I'm asked "How old are you?" enter some number then press enter i get this error message:

Have a coupon? Y/N Exception in thread "main" java.lang.NullPointerException at Demo.main(Demo.java:16)

Please help. Am i right in thinking it has something to do with the char reply variable not being initialised? but then i think thats what line 16 is doing so that cant be it? or is something even simpler like a typo that im missing?

Thanks heaps
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Tom Keegan wrote:I'm asked "How old are you?" enter some number then press enter i get this error message:

Hmm... after this question you enter yourage[in numeric] and space and enter either Y or N.
actually your question should be *System.out.print("How old are you? Have a coupon? Y/N ");*

i.e, example 26 Y

from Javadoc of findInLine :

Attempts to find the next occurrence of a pattern constructed from the
specified string, ignoring delimiters.

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to JavaRanch
Tom Keegan
Greenhorn

Joined: Dec 30, 2010
Posts: 3
Hey Seetharaman, thanks for the quick reply.

Tried what you said, but got the same error message.


Hmm... after this question you enter yourage[in numeric] and space and enter either Y or N.
actually your question should be *System.out.print("How old are you? Have a coupon? Y/N ");*

i.e, example 26 Y


Isnt that what lines 15 and 16 are doing? the first system.out asks how old are you then second system.out asks have a coupon?

Cheers mate.

Glad to be here
Rob McBryde
Greenhorn

Joined: Dec 18, 2010
Posts: 16
reply = myScanner.findInLine(".").charAt(0);


I would think that this line is searching for a full stop in your command window then attempts to find the first character in the string following this. It's as if the program is expecting you to type your age, followed by a fullstop. However, this would cause a java.util.InputMismatchException when myScanner attempts to read the next age as an integer.

If you want a quick fix to work around this you could simply replace line 16 with the following:

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Tom Keegan wrote:
Tried what you said, but got the same error message.

what is your input after the program started?
Rob McBryde
Greenhorn

Joined: Dec 18, 2010
Posts: 16
Hi Tom,

I have just run the following code and was able to successfully test it with numerous valid test data. The only difference I made to the program from your existing post is on line 16.






Example run of the program :

How old are you? 25
Have a coupon? Y/N y
Please pay $7.25. Enjoy the show!

Tom Keegan
Greenhorn

Joined: Dec 30, 2010
Posts: 3
Thanks Rob, the change you made to line 16 solved the problem.

The code i posted was from the book.
Is it a typo or am i missing something? am i right in guessing that FindInLine is the wrong method to be using in this case?

Thanks for the quick help guys. Much appreciated
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

findInLine may return null. If you check the return value before calling charAt(0) on it you'd be safe as well.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Don't get me started about those stupid light bulbs.
 
subject: exception in thread "main" error