• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.NullPointerException in main  RSS feed

 
Kwin Malama
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.  Welcome to the ranch!

Lets take a look at that error message:



NullPointerException - means you are getting a "null" value somewhere.  Either you haven't initialised a variable, or it is returning null, when you think it should be something else.
It points at line 15.

Line 15 in your code:


Things that could be null in this line of code
- myScanner
- myScanner.findInLine(".")

myScanner isn't null, because it got initialised on line 6.
So myScanner.findInLine(".") must be null.

What is this line of code meant to do?
Why use the method findInLine ?
Why is it a "." ?


 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You are using the findInLine() method incorrectly. Use myScanner.next().charAt(0) instead.
 
Kwin Malama
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Thank you so much for the reply's

System.out.print("Do you have a Coupon?(Y/N)");
reply = myScanner.findInLine(".").charAt(0);

is meant to capture the uses response when they select Y or N

So I suppose I need to read more on how to use this next()  method.

I have used this ......myScanner.next().charAt(0) and it has worked perfectly.

Thank you

 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kalisha Malz wrote:... I have used this ......myScanner.next().charAt(0) and it has worked perfectly.

What would happen if the user just pressed Return/Enter without first entering Y or N?
 
Kwin Malama
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well its supposed to run the following code of which its not doing  and its not picking the price

if (reply !='Y' && reply != 'y' &&
reply !='N' && reply !='n'){
System.out.println("Next Serious Customer");

still troubleshooting
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but that's after line 15.  What happens on line 15?  What does findInLine() do?
 
Kwin Malama
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"So findInLine(“.”)
tells the computer to find the next character of
any kind that the user types on the keyboard.
When you’re trying to input a single character,
findInLine(“.”) is mighty useful"

Thats an except from the book Beginning Java Programming for Dummies.

That line is supposed to pick Y or N reply?

I guess am so lost? I like your questioning its making me ponder on what exactly am doing
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kalisha Malz wrote:So findInLine(“.”) tells the computer to find the next character of any kind that the user types on the keyboard.

The documentation says: If the pattern is found before the next line separator, the scanner advances past the input that matched and returns the string that matched the pattern. If no such pattern is detected in the input up to the next line separator, then null is returned and the scanner's position is unchanged.

Your code needs to be prepared to deal with the case where the users does not enter any character, and just presses Enter on the keyboard, otherwise myScanner.findInLine(".").charAt(0) will fail with a NullPointerException.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a lot of confusion about Scanner's idea of next line. It is obvious that you go on to a new line and that is the next line. But the obvious explanation is wrong. The “line” is the remainder of the current line. If you enter a number 123 and push the enter key, the rest of the line remaining is the empty String "" and even findInLine(".") will not find anything in an empty String. So you get null returned. You can try a nextLine call (as in this old post) to get onto the next line. Or you can try a different find method. I am not used to the findXXX methods, I am afraid.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The “line” is the remainder of the current line.

Exactly.  I think the confusion here comes from the fact that the previous call to scanner.nextInt() consumes whatever digits were typed for the age, but it doesn't consume the newline characters after that.  So it doesn't actually go on to the next line.  And when findInLine(".") comes along after that, it finds the newline characters right away, and immediately identifies that as the end of the line it's looking for.  Very counterintuitive.  Good explanation, Campbell!
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the nextXXX (nextInt, next, nextByte, etc, but not nextLine) work by reading up to the next token (in the default case this is whitespace), but not including the next token.

nextLine is not part of this collection of methods.
It doesn't work with tokens at all, and simply reads up to and including the next new line character.
In the case of the default token used this can appear to be the same thing, but isn't at all.

Try comparing the output of next() and nextLine() with the input of "some string or other".

That gives the output (italics is input):
some string or other
some
some string or other
some string or other

If you remove the first call to nextLine() you get:
some string or other
some
string or other

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:. . . scanner.nextInt() consumes whatever digits were typed for the age, but it doesn't consume the newline characters after that.
Exactly. You have to push the enter key to get the int read as input, but the CR/LF or whatever counts as whitespace, which is included in the default delimiter, and is therefore not consumed, as you said.
. . .  Good explanation, Campbell!
Thank you. Lots of bitter experience with Scanner and input mismatch exceptions after nextLine. So many books will tell you that nextLine reads the next line, even though the nextLine() documentation doesn't say that at all.
the nextLine documentation wrote:This method returns the rest of the current line ...
I remember Winston once said the problem with Scanner is that there is so little documentation about it or tutorials. I like Scanner (I know lots of people don't), but it does take some learning of its more obscure features.

My suggestion of next().charAt(0) worked because the line end characters were consumed looking for the next token. I don't know whether this method would work better than findInLine; you would have to enter a number of characters to look for and see whether the desired text is found within that many characters. I would have thought the chances of y or n being found within (say) 20 characters is probably pretty high, though you can produce an Exception if you push space or enter often enough.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can see how I've used the Scanner class to create a simple text prompting class here:

https://github.com/ksnortum/Prompter
 
Risha Rommi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI, no mistakes in Eclipse in the synthax highlighting edit view but the program gives the mistake above ps. there is a method to fast TEST the book c.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!