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

Hangman game

Hi I was making the Hangman game myself without looking to the videos

I got the error:

Exception in thread "main" java.lang.NullPointerException at Prompter.makeGuess(Prompter.java:13) at Hangman.main(Hangman.java:8)

my code :

public class Hangman {

    public static void main(String[] args) {

        Prompter prompter = new Prompter();
        Game game = new Game("cow");
        prompter.makeGuess();








    }

}





import java.io.Console;


public class Prompter {

    public Prompter(){
    }

    public void makeGuess() {
        Console console = System.console();
        String guess = console.readLine("Make a guess!");

        }


}





public class Game {
    private String mWord;

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





}

It's complaining about line 13 in your prompter class. Something not being set. Something coming up null.

It is : String letterAsString = console.readLine("Make a guess!"); But I don't see the problem there

Are you running it in Workspaces or locally?

3 Answers

Eclipse has issues with java.io.Console. You need to use System.in and System.out instead:

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.printf("Make a guess: ");
String guess = bufferedReader.readLine();

Locally in Eclipse

thanks a lot I just found it out since here it's working I tried to change it to yours and no errors anymore but it doesnt ask me for an input either ok, maybe its just better to work in workspace but now I just want to have it work in Eclipse too, just because it should be possible :D