N/APosted on - 09/10/2016
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?
How to fix java.lang.NullPointerException on Eclipse LUNA
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
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String guessAsString = reader.readLine();