Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialOussama DJEDIDI
4,599 Pointsjava.lang.NullPointerException on the eclipe IDE
I tried run code on eclipse (Having some trouvle with internet), and I get this :
You have 7 tries left to solve --------- Exception in thread "main" java.lang.NullPointerException at Prompter.promptForGuess(Prompter.java:23) at Prompter.play(Prompter.java:13) at Hangman.main(Hangman.java:7)
Oussama DJEDIDI
4,599 PointsThis is the function for the hagman game of the Java course (I thought the question would be posted there under the video). The concerned line is :
String guessAsString = console.readLine("Enter a Letter: ");
public boolean promptForGuess(){
Console console = System.console();
boolean isHit = false;
boolean isValideGuess = false;
while(! isValideGuess){
String guessAsString = console.readLine("Enter a Letter: ");
char guess = guessAsString.charAt(0);
try {
isHit = mGame.applyGuess(guess);
isValideGuess = true;
}catch(IllegalArgumentException iae){
console.printf(iae.getMessage() + " Pleae try again.\n");
}
}
return isHit;
}
Tristan Smith
3,171 PointsThanks for posting the code. Do you recall what course and video this was in? I recall going through this, but for some reason I can't find the challenge/task on the course.
Oussama DJEDIDI
4,599 PointsSorry for my late answer (time zone difference). The code is from the Java Object Course, I believe it is the last part of it.
Tristan Smith
3,171 PointsNo worries. I wasn't able to recreate the NullPointerException though, and when I put your code in the workspace for [1] it worked as it should.
If possible, could you post your entire prompter class? (Prompter.java)
Found the vids:
So the prompter class that you seem to be working with is here [1], around 7-8 minutes he adds more to it.
But here [2] at 2-3 minutes he begins to refactor the code and fixes a bug.
[1] https://teamtreehouse.com/library/java-objects/delivering-the-mvp/validation
[2] https://teamtreehouse.com/library/java-objects/delivering-the-mvp/refactoring
Oussama DJEDIDI
4,599 PointsThanks for the answer ! The code worked for e just fine, my problem wads when I tried to run it in eclipse IDE not the workspace
1 Answer
Steve Hunter
57,712 PointsHI there,
The IDE may not come with a console
object as standard. This will create a null pointer if you try to use a readLine
method. Your code will work in Workspaces, which does provide a console
but not in the IDE.
As a workaround, I'd suggest using a Scanner
instance. I'll see if I can find a link that may help you out.
Steve.
Oussama DJEDIDI
4,599 PointsThanks ! I see now ! When importing, it works :D
Tristan Smith
3,171 PointsThank you, Steve! (I was testing it in workspaces, lol...).
Tristan Smith
3,171 PointsTristan Smith
3,171 PointsHi, could you post the code you're working with? It'll help us help you. :-)