Unquote (Java), Cumulative Project 4, " non-static method isCorrect() cannot be referenced from a static context" Error

Link to See the Exercise I am at.

*So I am currently in the Game Logic Pt. II of the Unquote game, and for the record: I dooo know where the error is coming from. That is not the issue I am dealing with. *
I am currently on task #14/21 and it reads:

14.But are they right?

Unquote features a ‘Submit’ button, and until the player presses this button, they may change their answer as many times as they wish.

By the time your code executes onAnswerSubmission(), the player has made their choice and your code has stored their selection inside of the Question object (you will handle this logic in the final Unquote project).

In onAnswerSubmission(), your first piece of business is to modify totalCorrect. If the player guessed correctly, you must add 1 to this member variable.
Refer to the methods in Question.java and use your convenience method from task 12 to check whether your code should add 1 to the totalCorrect member variable.
HINT PROVIDED:
Use getCurrentQuestion() to recover the Question object for which the player wishes to submit their final answer.

Then, in combination with an if statement, use Question.isCorrect() to determine whether the player chose the right answer.

Here is my code for that Task:

void onAnswerSubmission() {
   Question getCurrentQuestion;
   if (Question.isCorrect()) {
       totalCorrect += 1;
      }

  }

When I run the code (not just this ofcourse but the whole file) I get this error:

MainActivity.java:42: error: non-static method isCorrect() cannot be referenced from a static context
if (Question.isCorrect()) {
^
1 error

Hopefully someone can help with this, I am unable to move on because of this task. In order to fully get the context of what is going on, unless if you have already done this project , ur most likely going to have to use my link to actually see the whole thing. Thanks.

Can you show the code where the isCorrect() function is defined?
I don’t have access but I can maybe figure it out with the code… (if you post the whole code much more likely).

Otherwise yea, the thing to do is to either make the method static, or to reference it in a different way.

Ok so here is the code to the Question.java class where the question tells us to refer too.

public class Question {
    int imageId;
    String questionText;
    String answer0;
    String answer1;
    String answer2;
    String answer3;
    int correctAnswer;
    int playerAnswer;

    public Question(int imageIdentifier, String questionString, String answerZero,String answerOne, String answerTwo, String answerThree, int correctAnswerIndex) {
        imageId = imageIdentifier;
        questionText = questionString;
        answer0 = answerZero;
        answer1 = answerOne;
        answer2 = answerTwo;
        answer3 = answerThree;
        correctAnswer = correctAnswerIndex;
        playerAnswer = -1;
    }

    boolean isCorrect() {
      return correctAnswer == playerAnswer;
    }
}

The method you asked for is on the very bottom.
And then just in case, the class I am actually in and where I am working on and where I am stuck is the MainActivity.java which is this one:

import java.util.ArrayList;

public class MainActivity {

    // TODO #1: add integer member variables here:
    int currentQuestionIndex;
    int totalCorrect;
    int totalQuestions;

    // TODO #2: add ArrayList member variable here:
    ArrayList<Question> questions = new ArrayList<Question>();

    // TODO #3 add startNewGame() here:
    void startNewGame() {
      Question question1 = new Question(921238, "How tall is the Eiffel tower?", "1024 ft", "1063 ft", "1124 ft", "1163 ft", 1);
      Question question2 = new Question(107343, "Who invented the computer algorithm?", "Charles Babbage", "John Carmack", "Alan Turing", "Ada Lovelace", 3);
      Question question3 = new Question(748294, "What is the name for the patch of skin found on your elbow?", "Elbow Skin", "Fascia Elbora", "Wenis", "Todd", 2);
      questions = new ArrayList<>();
      questions.add(question1);
      questions.add(question2);
      questions.add(question3);
      totalCorrect = 0;
      totalQuestions = questions.size();
      Question firstQuestion = chooseNewQuestion();
      //displayQuestion(firstQuestion);
      //displayQuestionsRemaining(questions.size());
    }
    // TODO #4 add chooseNewQuestion() here:
    Question chooseNewQuestion() {
      int randomNum = generateRandomNumber(2);
      currentQuestionIndex = randomNum;
      return questions.get(currentQuestionIndex);

    }
    // TODO #5 add getCurrentQuestion() here:
    Question getCurrentQuestion() {
     return questions.get(currentQuestionIndex); 
    }
    // TODO #6 add onAnswerSubmission() here: 
    void onAnswerSubmission() { //I AM STUCK HERE
      Question getCurrentQuestion;
      if (Question.isCorrect()) {
        totalCorrect += 1;
      }

    }

    int generateRandomNumber(int max) {
      double randomNumber = Math.random();
      double result = max * randomNumber;
      return (int) result;
    }

    String getGameOverMessage(int totalCorrect, int totalQuestions) {
        if (totalCorrect == totalQuestions) {
            return "You got all " + totalQuestions + " right! You won!";
        } else {
            return "You got " + totalCorrect + " right out of " + totalQuestions + ". Better luck next time!";
        }
    }
}

I am stuck on the onAnswerSubmission() method

Hey Miguel,

I think the issue is 1. Question getCurrentQuestion; and 2. if (Question.isCorrect())

 void onAnswerSubmission() {
      Question getCurrentQuestion;
      if (Question.isCorrect()) {
        totalCorrect += 1;
      }
    }

Follow the format you have previously used for no. 1. Notice, getCurrentQuestion alone isn’t alluding to the method… so check your code because you make this type of declaration properly before.

For no. 2… well, you’re basically asking if “Question” isCorrect(). But where do you declare “Question”? Solving no.1 should make it clear what you need to do for no. 2.

Don’t hesitate to come back for more questions! I can’t give the answers away for you though sorry :sweat_smile:

1 Like

Nah man it is ok. I know the jist to all of this isn’t just giving away the answers. So I think I got it.
What I ended up wanting was the error:

Error: Main method not found in class MainActivity, please define the main method as:
public static void main(String args)
or a JavaFX application class must extend javafx.application.Application

Cause I know that this file where I am at is not the one with the main method. And I edited my code so now it looks like this:

    void onAnswerSubmission() {
      Question currentQues = getCurrentQuestion();
      if (currentQues.isCorrect()) {
        totalCorrect += 1;
      }
    }

Hopefully this is also what you meant in terms of what it looked like I should have done. It makes sense. And now the error(s) are gone. I just got the one I mentioned above, which in reality isn’t really much of an error in this case since this file doesnt have the main method and I am aware of that.

Yea that’s the correct way to do it.

And adding a main method is a breeze so it seems like it’s all good then!

1 Like