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 trialLuan Torres
1,172 PointsException in thread "main" java.lang.NullPointerException
Exception in thread "main" java.lang.NullPointerException
at Game.getRemainingTries(Game.java:22)
at Hangman.main(Hangman.java:7)
at Game.getRemainingTries(Game.java:22) : public int getRemainingTries() { return MAX_MISSES - misses.length(); }
at Hangman.main(Hangman.java:7): while (game.getRemainingTries() > 0){ prompter.displayProgress(); prompter.promptForGuess(); }
anybody knows why it is not working?
2 Answers
Linda de Haan
12,413 PointsA NullPointerException is thrown when a method is being used on a variable that hasn't been initialized. In this case the getRemainingTries() method is trying to use the length() method on the misses variable, but it hasn't been initialized, so it is null.
You can solve this by setting a default value in the constructor like this:
misses = "";
Luan Torres
1,172 PointsThank you Linda :)
Luan Torres
1,172 PointsLuan Torres
1,172 Pointsmy progress: https://w.trhou.se/53vpime5vp