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) Creating the MVP Prompting for Guesses

What is going on?

I tried to run on IntellJ and this is what I got

Prompter.promptForGuess(Prompter.java:15) at nyc.c4q.personabe1984.Hangman.main(Hangman.java:10) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

then when I tried to run console I got

javac Hangman.java && java Hangman Hangman.java:7: error: cannot find symbol Game game = new Game("Treehouse"); ^ symbol: class Game location: class Hangman Hangman.java:7: error: cannot find symbol Game game = new Game("Treehouse"); ^ symbol: class Game location: class Hangman Hangman.java:9: error: cannot find symbol Prompter prompter = new Prompter(game); ^ symbol: class Prompter location: class Hangman Hangman.java:9: error: cannot find symbol Prompter prompter = new Prompter(game); ^ symbol: class Prompter location: class Hangman 4 errors Hans (master *+) personabe1984 $ javac Game.java Hans (master *+) personabe1984 $ javac Prompter.java Prompter.java:8: error: cannot find symbol private Game mGame; ^ symbol: class Game location: class Prompter Prompter.java:10: error: cannot find symbol public Prompter(Game game){ ^ symbol: class Game location: class Prompter 2 errors

9 Answers

Alright so, i am literally in the same spot and i literally had the exact issue. It was answered in this thread: https://teamtreehouse.com/community/how-to-fix-javalangnullpointerexception

You need to import a scanner and replace the console with scanner and it works perfect.

  Scanner console = new Scanner(System.in);
        System.out.println("Enter a letter: ");
        String guessAsString = console.nextLine();

Can you please post your code?

public class Hangman {

public static void main(String[] args){

    Game game = new Game("Treehouse");

    Prompter prompter = new Prompter(game);
    boolean isHit = prompter.promptForGuess();

    if(isHit){
        System.out.println("is a hit");
    }else{
        System.out.println("is a miss");
    }
}

}

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.applyGuesses(guess);
}

}

public class Game { private String mAnswer; private String mHits; private String mMisses;

public Game(String answer){
    mAnswer = answer;
    mHits = "";
    mMisses = "";
}

public boolean applyGuesses(char letter){
    boolean isHit = mAnswer.indexOf(letter) >= 0;
    if(isHit){
        mHits += letter;
    }else{
        mMisses += letter;
    }
    return isHit;
}

}

The console object is not sitting well when I run it on intelliJ so when I tried to do it on terminal I still get the errors above.

Thanks. That solved the problem. But do you know why we cannot use the Console object?

never mind apparently even with the import the Console object is not available on IDE. So strange.

Because we are not running the program in the Console, we are running it in the IDE. Its explained in that thread better than i could explain it.