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

I can't get away from this NullPointerExpection

I keep getting a nullPointerException and it points me to the following points: Game class line 72 (getCurrentProgress()), Game class line 83 (isSolved) Prompter class line 25 (play())

Note: Since I'm using the IntelliJ IDEA IDE, I'm importing BufferedReader instead of System.console.

Edit: Put the prompter class twice. Sorry about that.

public class Main {

    public static void main(String[] args) throws Exception {

        System.out.printf("Welcome to Hangman!");
        Game game = new Game("program");
        Prompter prompter = new Prompter(game);
        prompter.play();

    }
}
public class Prompter
{
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    private  Game game;

    public  Prompter(Game Game)
    {
        game = Game;
    }



    public void play() throws Exception {

        char letter;
        while (game.getRemainingLives() > 0 && !game.isSolved())
        {
            displayProgress();
            promptForGuess();



        }
        if(game.isSolved())
        {
            System.out.printf("Congratulations! You've won the game with %d tries remaining.", game.getRemainingLives());
        }

        else
        {
            System.out.printf("You lose! Game over! The answer is %s", game.Answer());
        }

        System.out.printf("Sorry! You lose. The answer is %s. Game over!", game.getWord());
        System.exit(0);
    }

    public boolean promptForGuess() throws Exception
    {

        boolean isHit = false;
        boolean isValid = false;
        char singuess;

        while(!isValid)
        {
            System.out.printf("Enter a letter: ");
            String guessAsString = bufferedReader.readLine();
            try {
                singuess = guessAsString.charAt(0);
                isHit = game.Guess(singuess);
                isValid = true;
            }

            catch (IllegalArgumentException e)
            {
                System.out.printf("ERROR: %s!", e);
            }
        }


        return  isHit;
    }

    public void displayProgress()
    {
        System.out.printf(" You have %s lives left. Solve this problem: %s", game.getRemainingLives(), game.getCurrentProgress());
    }


}
public class Game {
    public static final int MAX_LIVES = 7;
    private String word;



    private  String hits;
    private  String misses;

    public String getWord() {
        return word;
    }

    private char validateGuess(char letter) throws Exception {
        if(!Character.isLetter(letter))
        {
            throw  new  Exception("That's not a letter! pick a letter");
        }
        letter = Character.toLowerCase(letter);
        if (misses.indexOf(letter) >= 0 || hits.indexOf(letter) >= 0)
        {
            throw new Exception("You already guessed the letter " + letter + ". Pick a letter you haven't chosen");
        }

        return letter;
    }

    public Game(String Word)
    {
        word = Word;
    }

    public boolean Guess(String letters) throws Exception
    {
        if(letters.length() == 0)
        {
            throw new IllegalArgumentException("Your answer is null. Please input");
        }
        char firstLetter = letters.charAt(0);
        return  Guess(firstLetter);


    }

    public String Answer()
    {
        return getWord();
    }

    public boolean Guess(char letter) throws Exception {
        validateGuess(letter);
        boolean isHit = word.indexOf(letter) >= 0;
        if(isHit)
            hits+= letter;
        else
            misses+= letter;

        return  isHit;
    }

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

    public boolean isSolved()
    {
        return getCurrentProgress().indexOf('-') == -1;
    }

    public int getRemainingLives()
    {


        return  MAX_LIVES - misses.length();
    }
}

You posted your Prompter.java twice, and not Game.java.

I've added the code for the Game class

1 Answer

You aren't initializing hits or misses in the Game class. It's probably best to initialize them as empty strings.

Thank you very much for your help. I'm new to the Treehouse. I'm also a relative greenthumb too. (Though I've dabble in a bit of C# and Android)