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 trial

Java Java Objects Creating the MVP Remaining Tries

Luan Torres
Luan Torres
1,172 Points

Exception 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
Linda de Haan
12,413 Points

A 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 = "";