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 trialGi Devs
12,171 Pointsnon-static method can't be referenced
I'm getting an error from my displayProgress
here is the error
Hangman.java:5: error: non-static method displayProgress() cannot be referenced from a static context
Prompter.displayProgress();
^
Hangman.java:12: error: non-static method displayProgress() cannot be referenced from a static context
Prompter.displayProgress();
^
and here are the 2 peices of coade
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("please enter a letter: ");
char guess = guessAsString.charAt(0);
return mGame.applyGuess(guess);
}
public void displayProgress() {
System.out.printf("try to solve: 5s \n", mGame.currentProgress());
}
}
public class Hangman {
public static void main (String[] args) {
Game game = new Game("code");
Prompter prompter = new Prompter(game);
Prompter.displayProgress();
boolean isHit = prompter.promptForGuess();
if (isHit) {
System.out.println("We got a hit!");
} else {
System.out.println("nope sorry");
}
Prompter.displayProgress();
}
}
[MOD: edited code block]
Brecht Philips
8,863 Pointsin the hangman class you made a syntax mistake You have Prompter.displayProgress(); => Prompter is your class and you named the object prompter try instead this prompter.dispalyProgress();
1 Answer
Steve Hunter
57,712 PointsYes, Brecht is correct. You have called your method on the class itself, rather than the instance of the class. So, chain your method calls on prompter
not Prompter
and that should fix those issues.
Steve.
Gi Devs
12,171 PointsGi Devs
12,171 PointsI got it to work by chaning mGame and displayProgress to statics but I feel like this may cause problems later on. . . whats the proper solution