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

Hector Torres
Hector Torres
4,802 Points

Can't solve this error....

Exception in thread "main" java.lang.NullPointerException
at Game.getRemainingTries(Game.java:35) >>> return MAX_MISSES - mMisses.length();
at Prompter.play(Prompter.java:10) >>> while (mGame.getRemainingTries() > 0) {...
at Hangman.main(Hangman.java:6 >>> prompter.play();

Any ideas why?

Hangman.java

public class Hangman {

    public static void main(String[] args) {
      Game game = new Game("treehouse");
      Prompter prompter = new Prompter(game);
      prompter.play();

    }
}

Prompter.java

import java.io.Console;

public class Prompter {
  private Game mGame;

  public Prompter(Game game) {
    mGame = game;
  }
  public void play() {
    while (mGame.getRemainingTries() > 0) {
      displayProgress();
      promptForGuess();
    }
  }

  public boolean promptForGuess() {
    Console console = System.console();
    String guessAsString = console.readLine("Enter a letter: ");
    char guess = guessAsString.charAt(0);
    return mGame.applyGuess(guess);
  }
  public void displayProgress() {
    System.out.printf("You have %d tries to solve:  %s\n", 
                      mGame.getRemainingTries(),
                      mGame.getCurrentProgress());
  }
}

Game.java

public class Game {
  public static final int MAX_MISSES = 7;
  private String mAnswer;
  private String mHits;
  private String mMisses;
  private String letter;

  public Game(String answer) {
    mAnswer = answer;
  }

  public boolean applyGuess(char letter) {
    boolean isHit = mAnswer.indexOf(letter) >= 0;
    if (isHit) {
      mHits += letter;
    } else {
      mMisses += letter;
    }
    return isHit;
  }

  public String getCurrentProgress() {
    String progress = "";
    for (char letter : mAnswer.toCharArray()) {
      char display = '-';
      if (mHits.indexOf(letter) >= 0) {
        display = letter;
      }
      progress += display;
    }
    return progress;
  }

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

EDIT: Added the code posts to original question for better readability. - Dane E. Parchment Jr. (Moderator)

1 Answer

Alfred Lindal Haug
Alfred Lindal Haug
1,231 Points

It seems that you're trying to reference your variables from the Game-class without instantiating them. A simple fix is maybe instantiating them with "". (private String whateverVariable = "";) . This seems to work in console for me, but for some reason my IDE still gives me errors.