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 Error in NetBeans :(

hi everybody, I don't know where is my mistakes

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

    public Game(String answer){
      mAnswer = answer;
      mHits = "";
      mMisses = "";
    }
    public boolean applyGuess(char letter){
      boolean isHit = mAnswer.indexOf(letter)>=0; 
      if(isHit){
        mHits += letter;
      } else {
        mMisses += letter;
      }
      return isHit;
    }
}
public class Hangman {
    public static void main(String[] args) {
        Game game = new Game("TreeHouse");
        Prompter prompter = new Prompter(game);
        boolean isHit = prompter.promptForGuss();
         if (isHit){
           System.out.println("we got a hit");
         } else {
           System.out.println("Sorry, that was a miss");
         }
    }  
}
import java.io.Console;
public class Prompter {
    private Game mGame;

    public Prompter(Game game){
      mGame = game;
    } 
    public boolean promptForGuss(){
      Console console = System.console();
      String guessAsString = console.readLine("Enter a letter: ");
      char guess = guessAsString.charAt(0);
      return mGame.applyGuess(guess);
    }
}
Simon Coates
Simon Coates
28,694 Points

If using an IDE, you should be able to set a breakpoint and use your debugging tools to step through (and inspect variables). The exception message should also give you a pretty clear indication. It's probably System.console that's returning null. Possible to use System.in (in some environments at least), but it's more complicated. a rather terrible demo that seems to work in eclipse:

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

public class Prompter {
    private Game mGame;

    public Prompter(Game game){
      mGame = game;
    } 
    public boolean promptForGuss(){
        //create the Scanner
        Scanner terminalInput = new Scanner(System.in);

        //read input
        System.out.println("Enter a line");
        String guessAsString = terminalInput.nextLine();
      System.out.println("you entered:"+guessAsString);
      char guess = guessAsString.charAt(0);
      return mGame.applyGuess(guess);
    }
}
Name:GoogleSearch orJonathan Sum
Name:GoogleSearch orJonathan Sum
5,039 Points

Simon Coates Oh yeah~ Thx man. I am having this problem too.

How do u know this solution?...i mean...what java course have u token in order to understand this solution?

Simon Coates
Simon Coates
28,694 Points

I'm not sure they cover it in treehouse. They expect you to develop in workspaces, where System.console() seems to work. System.in shows up in a lot of online java courses. They typically use System.out for output and System.in for input (they're corresponding input and output streams if you look at the System.class documentation). However, the output class is difficult to use, so you typically interact with a Scanner class instead.

Name:GoogleSearch orJonathan Sum
Name:GoogleSearch orJonathan Sum
5,039 Points

@Simon Coates I see you have known that much of stuff. However, are you able to find a job if you have known that much of Java?

Simon Coates
Simon Coates
28,694 Points

knowing syntax might get an entry level job. Most java jobs requires additional knowledge (android, spring, hibernate...), experience, familiarity with versioning (GIT), etc. Treehouse covers a lot of this.

2 Answers