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 trialChristopher Stutler
3,080 PointsError when trying to run in JShell
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)
Christopher Stutler
3,080 PointsThe only thing else I typed was /open Game.java
markmneimneh
14,132 Pointsthe error is in the applyGuess method of the Game class. Post the content here so someone can help you.
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsYou 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
3,080 PointsOkay, 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
3,080 PointsI 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.
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsBrendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsWhat are you typing into the console prior to that? I tried running your code and it seemed to work on my end: