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
Milan Slaměnik
1,739 Pointsnon-static method promptForGuess()
I keep getting an error message in the console from this code below.
public class Hangman {
public static void main(String[] args) {
// Enter amazing code here:
Game game = new Game("treehouse");
Prompter prompter = new Prompter(game);
boolean isHit = Prompter.promptForGuess();
if(isHit){
System.out.println("We got a hit");
}else{
System.out.println("Whoops miss");
}
}
}
Here is the error message I get.
Hangman.java:7: error: non-static method promptForGuess() cannot be referenced from a
static context
boolean isHit = Prompter.promptForGuess();
Here is the Prompter.java:
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.applyGuess(guess);
}
}
1 Answer
Daniel Hartin
18,106 PointsHi Milan
just a typo here. You have used the capital P in the line
boolean isHit = Prompter.promptForGuess();
which references the class not the object which you created on the line above simply replace it for a lowercase p and you're all set.
Daniel
Milan Slaměnik
1,739 PointsMilan Slaměnik
1,739 PointsThanks ! :)