Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exception in thread "main" error

 
Tom Keegan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch
 
Tom Keegan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Keegan wrote:
Tried what you said, but got the same error message.

what is your input after the program started?
 
Rob McBryde
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
findInLine may return null. If you check the return value before calling charAt(0) on it you'd be safe as well.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic