1

I'm new to java so I might be missing something obvious. I'm trying to create a simple command-line game in java. I use two classes:

http://pastebin.com/wRgqgJQP

http://pastebin.com/0rXbxdiJ

The first handles user inputs, the second runs a math question game. When I try to run the jar file (the eclipse file runs fine), I get an error - can't be launched, and the following console print out:

Exception in thread "main" java.lang.NullPointerException at game.GameHelper.getUserInput(GameHelper.java:12) at game.MultGame.createGame(MultGame.java:18) at game.MultGame.main(MultGame.java:12) 

Any ideas how to fix this? I'm thinking that the problem is related to using the sysout print thing...but Im not sure. Thanks!

4 Answers 4

2

A NullPointerException indicates that a variable was null when being used with either a . or an array reference like [0].

The stack trace tells us it happened "at game.GameHelper.getUserInput(GameHelper.java:12) ". Your source listing has this line at line 12 in GameHelper.

if (inputLine.length() == 0) 

There is only one . telling us that inputLine was null. How did that happen? Well, it was assigned in line 11.

inputLine = is.readLine(); 

So. readLine() returned null. How did that happen? Well, from http://download.oracle.com/javase/1.4.2/docs/api/java/io/BufferedReader.html#readLine()

Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

So the end of the stream has been reached. The stream was constructed from System.in, so additional information is needed to tell why that may be.

Sign up to request clarification or add additional context in comments.

2 Comments

ok, makes sense, thanks! So if I understand right, the problem is that the program has decided the line has been read already, before any input is given? How should I go about allowing the user to enter text?
No, that you have reached the end of the input stream. This may be the case if you launch without a console window attached because then there is no place for the user to type text. You must provide details of how you launch your program.
1

The way to debug ANY nullpointerexception

1) Go to the line. 2) Look at each method call on that line - is is possible that a method called on an object where the object is null ?

a=null ; a.setX("X"); will result in a null pointer exception. 

In your specific case, the line "if (inputLine.length() == 0)" is throwing a null pointer exception. Thus, you should make sure that "inputLine" is not null ....

Comments

1

Any particular reason why you aren't using the Scanner class?

package game; import java.util.Scanner; public class GameHelper { public String getUserInput(String prompt) { System.out.print(prompt + " "); Scanner scan = new Scanner(System.in); String inputline = scan.nextLine(); return inputLine.toLowerCase(); } } 

And if all you ever want it to do is use the result to ParseInt, you could change

String inputLine = scan.nextLine() 

to

int inputNumber = scan.nextInt() 

and obviously change the return type from String to int

1 Comment

I guess...because I'd never heard of it until now? haha. I'll give it a try. thanks!
0

Try this

package game; import java.io.*; public class GameHelper { public String getUserInput(String prompt) { String inputLine = null; System.out.print(prompt + " "); try { BufferedReader is = new BufferedReader(new InputStreamReader( System.in)); inputLine = is.readLine(); if (inputLine == null) return null; } catch (IOException e) { System.out.println("IOException: " + e); } return inputLine.toLowerCase(); } } 

To fix your other error surround

 numProbs=(Integer.parseInt(numProbsReader)) 

in try/catch so like this

try{ numProbs=(Integer.parseInt(numProbsReader)) }catch(Exception e){System.err.println("Invalid Input");} 

and that should help

1 Comment

thanks! I did that, and the error is now: Exception in thread "main" java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Integer.java:417) at java.lang.Integer.parseInt(Integer.java:499) at game.MultGame.createGame(MultGame.java:19) at game.MultGame.main(MultGame.java:12)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.