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 (Retired) Creating the MVP Current Progress

Gi Devs
Gi Devs
12,171 Points

non-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

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("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());
      }
}
hangman.java
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]

Gi Devs
Gi Devs
12,171 Points

I 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

Brecht Philips
Brecht Philips
8,863 Points

in 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

Yes, 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.