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 Current Progress

Hugo Hayes
PLUS
Hugo Hayes
Courses Plus Student 13,146 Points

NullPointerException

When I followed everything Craig did in the workspace, and try to run Hangman.java. It doesn't have any compile error, but it gives back an exception that says java.lang.NullPointerException at Game.getCurrentProgress(Game.java : 25) at Prompter.displayProgress(Prompter.java : 21) at Hangman.main(Hangman.java : 8) which I think there is something wrong with if(hits.indexOf(letter) != -1){ Can somebody be nice enough to reply to this?

class Game{
    private String answer;
    private String hits;
    private String misses;

 public Game(String answer){
        this.answer = answer;
        }

 public boolean applyGuess(char letter){
        boolean isHit = answer.indexOf(letter) != -1;
        if (isHit){
        hits += letter;
        }
      else{
        misses += letter;
        }
        return isHit;
    }

    public String getCurrentProgress(){
        String progress = "";
        for(char letter : answer.toCharArray()){
        char display = '-';
        if(hits.indexOf(letter) != -1){
        display = letter;
            }
         progress += letter;
        }
        return progress;
    }
}
public class Hangman {

  public static void main(String[] args) {
    // Your incredible code goes here...
    Game game = new Game("treehouse");
        Prompter prompter = new Prompter(game);

        prompter.displayProgress();

            boolean isHit= prompter.promptForGuess();
            if (isHit){
            System.out.print("It's good!!");
            }else{
            System.out.print("Oops,missed...");
            }

  }
}
import  java.util.Scanner;

class Prompter{
    private Game game;

    public Prompter(Game game){
    this.game = game;

    }

    public boolean promptForGuess(){
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a letter: ");
        String guessInput = scanner.nextLine();
        char guess =guessInput.charAt(0);
        return game.applyGuess(guess);
    }

    public void displayProgress(){
        System.out.printf("Try to solve: %s%n",
                                            game.getCurrentProgress());
    }

}

We need to see the code in order to troubleshoot and debug.

It would be easiest to give a snapshot of your workspace if you're doing it in workspaces.

Your welcome Hugo. Glad I can help.

1 Answer

The reason why you are getting the NullPointerException is because when you start the Hangman game, the variable "hits" in the Game class is not initialized, it is NULL. So when if(hits.indexOf(letter)) reaches hits, it tries to assign a value to a NULL. And it's the same case with the misses variable. So you need to initialize both the "hits" and "misses" variable to empty strings in the Game constructor and you should be fine.

changed comment to answer

Hugo Hayes I moved omid's answer if you'd like to select best answer.

Thanks a lot for the clarification!