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 Creating the MVP Storing Guesses

Christopher Stutler
Christopher Stutler
3,080 Points

Error when trying to run in JShell

https://w.trhou.se/lw2x9c986a

I've gone over the code three or four times and as far as I can tell everything lines up fine. but I am getting the following error. Please helpl

game.applyGuess('t');
| java.lang.NullPointerException thrown:
| at Game.applyGuess (#1:12)
| at (#11:1)

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

What are you typing into the console prior to that? I tried running your code and it seemed to work on my end:

jshell> Game game = new Game("treehouse");                                                                                      
game ==> Game@56ef9176                                                                                                          

jshell> game.applyGuess('t');                                                                                                   
$3 ==> true                                                                                                                     

jshell>                                                                                                                         
Christopher Stutler
Christopher Stutler
3,080 Points

The only thing else I typed was /open Game.java

the error is in the applyGuess method of the Game class. Post the content here so someone can help you.

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

You need to create an instance of a Game class first, which is what this line is doing:

Game game = new Game("treehouse");           

Then you can try game.applyGuess('t')

2 Answers

Christopher Stutler
Christopher Stutler
3,080 Points

Okay, It's working now. I'm not quite sure what I did differently as I spent meticulously checked everything the night I was working on it, but the code it working as shown in the example. I appreciate the help.

Christopher Stutler
Christopher Stutler
3,080 Points

I placed the instance into the counsel before running game.applyGuess

Here is my game class

class Game {
  private String answer;
  private String hits; 
  private String misses; 

  //Forces the games to throw an answer.  Note the instance field is defined by the constructer this answer, seperating it from the arguement in the braces.

  public Game(String answer) {
    this.answer = answer; 
    hits = "";
    misses = "";
  }

  public boolean applyGuess(char letter) {
    boolean isHit = answer.indexOf(letter) != -1;
    if (isHit) {
      hits += letter;
    } else {
      misses += letter;
    }
    return isHit;

  }

}

I'm sure what I'm missing is stupid, but after cruising through the first module and a half, I seem to be stumbling here.