How to fix java.lang.NullPointerException on Eclipse LUNA

Asked By 0 points N/A Posted on -
qa-featured

I'm currently using Eclipse LUNA working on a prompting-guess activity on a Java Basic Course in J2EE. How do I deal with such errors:

“Exception in thread "main" java.lang.NullPointerException at projectHangMan.Prompter.promptForGuess(Prompter.java:13) at projectHangMan.Hangman.main(Hangman.java:10)”

It seems that the error is mainly from this part:

String guessAsString = console.readLine("Enter a letter: ");

Can someone suggest a possible solution to this?

SHARE
Answered By 1065 points N/A #99232

How to fix java.lang.NullPointerException on Eclipse LUNA

qa-featured

The NullPointerException you're facing is due to the Java Code you are running in an IDE platform, where you do not have access to the console object. Because in a console environment, you have the liberty to go through the Command Prompt to the directory where you can run the java/class files.

If you want to run your code, first of all you have to use a console environment.

If you wish to continue with Eclipse environment,  it is better that you use Scanner , InputStreamReader or BufferedReader classes to get input from user console.

For output, you can resort to predefined IO functions like System.out.printf()

For example:

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

String guessAsString = reader.readLine();


 

Related Questions