• Post Reply Bookmark Topic Watch Topic
  • New Topic

scanner not reading ??? null pointer error  RSS feed

 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm having problems with Scanner being able to read the first time I call it and then not reading or reading in a null the second time I call it...can anybody tell me why?
this is the main of a game which must be played from the terminal so I have to keep this terminal closing and opening throughout the whole game...

now i post the code up to where the error is...



I'm getting a java.lang.NullPointerException on line 26...which means String lev is still null, which means scanner did not scan anything even after the player inserterd "easy" or "medium" from the console...
what is it that i'm doing wrong ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Two points. One. There is no reason to create so many scanner instances to read from standard input. Just create one at the beginning, and use it for the whole program. And Two, never ever close standard input. You didn't open the standard input file descriptor, so why are you closing it? And with standard input closed, how are you going to access standard input now?

Henry
 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Two points. One. There is no reason to create so many scanner instances to read from standard input. Just create one at the beginning, and use it for the whole program. And Two, never ever close standard input. You didn't open the standard input file descriptor, so why are you closing it? And with standard input closed, how are you going to access standard input now?

Henry


sorry for the noob question but what do you mean by never ever close standard input? what do you mean by standard input here? you mean the Scanner object? and what do you mean by the standard input file descriptor? y
 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so just by eliminating all the other scanners and using one and not closing it I guess is it working... by the standard input file descriptor do you by the type that scanner has to read in ? well I guess for string .hasNext() should be enough or I'm I wrong ?
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mike Vigor wrote:what do you mean by never ever close standard input?

What you did on line 14 and again on line 32. Don't do that.

what do you mean by standard input here? you mean the Scanner object?

Standard input => System.in

When you create a Scanner object like this:
you are essentially telling Java, "Create a Scanner that reads from the standard input stream."
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mike Vigor wrote:. . . I'm getting a java.lang.NullPointerException on line 26...which means String lev is still null, which means scanner did not scan anything even after the player inserterd "easy" or "medium" from the console...
what is it that i'm doing wrong ?
I always thought it is impossible to get null from a Scanner object, other than using the two (overloaded) methods start with “find”:- 1 2. For anything else, it is impossible to get null from a Scanner. If it cannot find the input you are looking for, it will simply throw one of two Exceptions:- 1 2, or if you have already closed the Scanner, this Exception.

So, what has happened? You closed a Scanner, found it threw an Exception because it was closed, created another Scanner, found that was throwing a different Exception and get rid of that Exception with this if block. That means you are not actually entering the block (System.in always has a “next” unless you have closed it). Then the compiler complains about lev might not have been initialised, so you give it a default value of null. And we have a whole chain of things where the Exceptions and compiler errors lead you up the garden path. You didn't use Eclipse, did you, and believe the little yellow mark about potential resource leak?

I shall repeat what Henry said yesterday with slight changes, which tells you all you need to know:-
One. . . . Just create one [Scanner] at the beginning, and use it for the whole program. And Two, never ever close [it].
That code structure leaves a lot to be desired, I am afraid. The block I quoted hasn't been indented correctly, and there are other indentation errors. You have lines much too long (e.g. line 9) which shou‍ld be broken into two lines. You also go the other way with too many empty lines together. You have badly‑named variables, e.g. n, lev.
There are also design problems. That code doesn't belong in the main method in the first place. Consider a setUpGame method in your game class.
I would probably use do loops instead of those two while loops. I think you will find do loops better express the intent of the program.
It would be a good idea to use an enumerated type for the game level. Much more object‑oriented than Strings.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!