exception in thread "main" error
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 14 years ago
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 :
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
Tom Keegan
Greenhorn
Posts: 12
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hey Seetharaman, thanks for the quick reply.
Tried what you said, but got the same error message.
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
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
posted 14 years ago
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:
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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:
posted 14 years ago
what is your input after the program started?
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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: 17
1
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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!
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
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 - OCEJPAD 6
How To Ask Questions How To Answer Questions
| Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |













