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 trialNuno Reis
23,956 PointsConsole error
Hi, I am following this Java tutorial but I am getting this weird console error and I can't figure what's wrong with my code. Here's the error I am getting from the console:
Picked up JAVA_TOOL_OPTIONS: -Xmx128m
Picked up _JAVA_OPTIONS: -Xmx128m
Picked up JAVA_TOOL_OPTIONS: -Xmx128m
Picked up _JAVA_OPTIONS: -Xmx128m
Exception in thread "main" java.lang.NullPointerException
at Game.getCurrentProgress(Game.java:24)
at Prompter.displayProgress(Prompter.java:20)
at Hangman.main(Hangman.java:7)
Hangman.java
public class Hangman {
public static void main(String[] args) {
// Your incredible code goes here...
Game game = new Game("treehouse");
Prompter prompter = new Prompter(game);
prompter.displayProgress();
boolean isHit = prompter.promptForGuess();
if (isHit) {
System.out.println("We got a hit");
} else {
System.out.println("Oops missed!");
}
prompter.displayProgress();
}
}
Prompter.java
import java.util.Scanner;
class Prompter {
private Game game;
public Prompter(Game game) {
this.game = game;
}
public boolean promptForGuess() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a Letter: ");
String guessInput = scanner.nextLine();
char guess = guessInput.charAt(0);
return game.applyGuess(guess);
}
public void displayProgress() {
System.out.printf("Try to solve: %s%n",
game.getCurrentProgress());
}
}
Game.java
class Game {
private String answer;
private String hits;
private String misses;
public Game(String answer) {
this.answer = answer;
}
public boolean applyGuess(char letter) {
boolean isHit = answer.indexOf(letter) != -1;
if(isHit) {
hits += letter;
} else {
misses += letter;
}
return isHit;
}
public String getCurrentProgress() {
String progress = "";
for (char letter : answer.toCharArray()) {
char display = '-';
if (hits.indexOf(letter) != -1 ) {
display = letter;
}
progress += display;
}
return progress;
}
}
Can anyone provide some help here? Thanks :)
1 Answer
Livia Galeazzi
Java Web Development Techdegree Graduate 21,083 PointsOn this line:
if (hits.indexOf(letter) != -1 ) {
You're calling the method indexOf() on the String 'hits'. But the variable 'hits' is only assigned to a string once you run applyGuess() and a letter matches. Before that the variable 'hits' does not have any string assigned to it. If you try to rund indexOf() on it, it will fail, because there is no string object to run this method. So you get a NullPointerException (because the variable points to a Null).
Two possible solutions:
- Initalize the variable 'hits' in the constructor by assigning it to an empty string
- Change your if condition to first check that the variable 'hits' is not null. If it's not null, then check if the string contains the letter.
Nuno Reis
23,956 PointsNuno Reis
23,956 PointsAh right, gotcha! Thanks Livia :)