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 (Retired) Creating the MVP Remaining Tries

Jonathan Mitten
PLUS
Jonathan Mitten
Courses Plus Student 11,197 Points

NullPointerException at Game.getRemainingTries line 35

As far as I can tell, I've correctly set up all the code, and reviewed videos and other code samples on this course to ensure I have everything written correctly.

However, when I run Hangman, I immediately get:

Exception in thread "main" java.lang.NullPointerException                                                                                            
        at Game.getRemainingTries(Game.java:35)                                                                                                      
        at Prompter.play(Prompter.java:11)                                                                                                           
        at Hangman.main(Hangman.java:8)       

Which refers to :

public int getRemainingTries() {
    return MAX_MISSES - mMisses.length();
}

I'm not sure how to debug this within the Treehouse Workspaces, or if it's a workspaces issue that's throwing the error.

Jennifer Leap
Jennifer Leap
21,702 Points

This suggest you have a null object somewhere--maybe a variable that hasn't been initialized? It's difficult to tell without seeing your full code.

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

One of the variables in getRemainingTries() is uninitialized. It's most likely mMisses, which would cause calling length() on the null variable to throw the exception. You probably forgot to set it to an empty string in the constructor.