Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Delivering the MVP Determining if It Is Solved

I'm trying to run the Hangman on eclipse but it is giving me an error with the play method. How do I fix this?

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();

    boolean isHit  = false;
    boolean isValidGuess = false;

    while(!isValidGuess){
    String guessAsString = console.readLine("Enter a letter: ");
    char guess = guessAsString.charAt(0);
        try{
                isHit = mGame.applyGuess(guess);
                isValidGuess = true;
        }catch(IllegalArgumentException ex){
            console.printf("%s  Please try again. \n", ex.getMessage());
        }
    }
    return isHit;//return mGame.applyGuess(guess);
}


public void displayProgress(){

    System.out.printf("You hava %d tries left to solve: %s\n", mGame.getRemainingTries(), mGame.getCurrentProgress());


}

}

You hava 7 tries left to solve: --------- Exception in thread "main" java.lang.NullPointerException at Prompter.promptForGuess(Prompter.java:35) at Prompter.play(Prompter.java:19) at Hangman1.main(Hangman1.java:9)

2 Answers

Simon Coates
Simon Coates
28,692 Points

is console null? i ran it in eclipse on my computer and console wasn't present. Might be able to use system.in in the event console is unavailable. There are finite options for something that is magically null, but presumably wasn't in the environment you created the code. I'd also suggest working out breakpoints with eclipse, so you can pause the code on an earlier line and examine variables.

tomtrnka
tomtrnka
9,780 Points

I dont know how about eclipse, but im doing it in IntelliJ IDEA, and this IDE does not like the: import java.io.Console;

Therefore instead of Console, I used: import java.util.Scanner; // equivalent to: import java.io.Console;

then in the code, you instanciate it: Scanner in = new Scanner(System.in); // equivalent to: Console console = System.console();

and then you prompt and store it like this: System.out.print("Enter a guess : "); String guessAsString = in.nextLine(); // equivalent to: String guessAsString = console.readLine("Enter a letter: ");

Thats all. Three rows of code.