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 Prompting for Guesses

java.lang.NullPointerException - i use eclipse and i get this errore

i gote error i line String guessAsString = console.readLine("Enter a letter: ");

import java.io.Console;

public class Prompter {

private Game mGame;

public Prompter(Game game){

   mGame = game;    
}

public boolean promptForGuess(){
    Console console = System.console();
    String guessAsString = console.readLine("Enter a letter:  ");
    char guess = guessAsString.charAt(0);
    return mGame.apllyGuess(guess);
}

}

4 Answers

Vincent Murray
Vincent Murray
11,300 Points

If you are using Eclipse

//import java.io.Console;
import java.util.Scanner;

public class Prompter{

    private Game mGame;

    public Prompter(Game game){
        mGame = game;

    }

    public boolean promptForGuess(){
        //Console console = System.console();
        Scanner kb = new Scanner(System.in);   // kb for Keyboard input can be anything
        //String guessAsString = console.readLine("Enter a letter :");
        String guessAsString = kb.nextLine();
        char guess = guessAsString.charAt(0);
        return mGame.applyGuess(guess);

    }

}

Then enter a letter in the console , and it will return a response

Thank you very much, you've been very helpful.

Vincent,

Thank you for the information and this did solve my problem, although can you direct me to a resource which can explain to me 'why' I get a NullPointerException when using Console instead of Scanner.

Vincent you saved me :) thanks a lot

Wait a min.. when I complie that window doesnt say anything. I think you have to add System.out.println how do I do that?

anyway when complier runs and if I type just t it works :D