• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why isn't my code working?  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get a null pointer exception when it's time to run the scanner so the user can insert the name.

Main class:

Secondary class:
 
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I don't see where you instantiate Scanner object.
 
Joshua Gallagher
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Welcome to the Ranch.

I don't see where you instantiate Scanner object.


Thank you, it worked!

Your answer was very useful to me!

Yeah, it was an honest mistake.
 
Liutauras Vilda
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome. Well done sorting that out
 
Joshua Gallagher
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:You are welcome. Well done sorting that out


Now I'm getting another error because of another class. Can you help me again?

It's also with the Scanner class.

This is the error:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Encounters.startEncounter(Encounters.java:37)
at Game.main(Game.java:8)

Main class:



The other class:

 
Liutauras Vilda
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to read the stacktrace and try to find error.
In the stacktrace look for the classes you defined and after the colon there is a line number where most likely things go wrong.
 
Joshua Gallagher
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:You need to read the stacktrace and try to find error.
In the stacktrace look for the classes you defined and after the colon there is a line number where most likely things go wrong.


Eyup, I did.

Things go wrong here:



Tryed scanning as String but no luck.
 
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception in thread "main" java.util.NoSuchElementException

Have you looked up the API doc for that class?  Sometimes the API doc for the exception class will have useful information.
The API docs are here:  http://docs.oracle.com/javase/8/docs/api/index.html


How does the Texts class use the Scanner?
 
Joshua Gallagher
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
Exception in thread "main" java.util.NoSuchElementException

Have you looked up the API doc for that class?  Sometimes the API doc for the exception class will have useful information.
The API docs are here:  http://docs.oracle.com/javase/8/docs/api/index.html


How does the Texts class use the Scanner?


It's right up there in the OP.

Apparently what was causing the problem was this:



But am I not supposed to close to scanner every time I use it?
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am I supposed to close to scanner every time I use it?

Not if it is wrapping System.in.  I believe that closes access to the keyboard entries for the rest of the program's execution.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:. . . Not if it is wrapping System.in.  I believe that closes access to the keyboard entries for the rest of the program's execution.
It does. You can then never reopen System.in.
You must beware of using
while (myScanner.hasNext()) ...
for System.in because this is (in theory) an infinite loop.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!