Cumulative Project 4 / non-static method onAnswerSubmission() cannot be referenced from a static context

Hi everyone!

I’m dealing with Cumulative Project 4, it seems I completed all the tasks. but in the end, when I’m trying to play the game I get an error:

“Main.java:9: error: non-static method onAnswerSubmission() cannot be referenced from a static context
MainActivity.onAnswerSubmission();”

Here is the code of my onAnswerSubmission() method:

    // TODO #6 add onAnswerSubmission() here
    void onAnswerSubmission() {
      Question currentPlayersQuestion = getCurrentQuestion();
      if (currentPlayersQuestion.isCorrect()) {
        totalCorrect = totalCorrect + 1;
      } else if (questions.size() == 0) {
      getGameOverMessage(totalCorrect, totalQuestions);
      startNewGame();
      } else if (questions.size() > 0) {
      chooseNewQuestion();
      questions.remove(currentQuestionIndex);
      }
      // displayQuestionsRemaining();
      // TODO: uncomment after implementing displayQuestion()
      // displayQuestion(getCurrentQuestion());
    }

Please help me to figure out the problem.

Maybe you can make onAnswerSubmssion() static? Or find a way not to reference it from static context… either way…

If you share the rest of the code I might be able to give a more complete answer…

My Main class:

public class Main {
    public static void main(String[] args) {
        MainActivity mainActivity = new MainActivity();
        mainActivity.startNewGame();
        System.out.println("Questions remaining: " + mainActivity.totalQuestions);
        Question currentQuestion = mainActivity.getCurrentQuestion();
        printQuestion(currentQuestion);
        // Play the game!
    }

    static void printQuestion(Question question) {
        System.out.println("Question: " + question.questionText);
        System.out.println("Option 1: " + question.answer0);
        System.out.println("Option 2: " + question.answer1);
        System.out.println("Option 3: " + question.answer2);
        System.out.println("Option 4: " + question.answer3);
    }
}

My MainActivity class:

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;

    // 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<Question>();
    questions.add(question1);
    questions.add(question2);
    questions.add(question3);
    int currentQuestionIndex = 0;
    int totalCorrect = 0;
    int totalQuestions = 0;
    Question firstQuestion = chooseNewQuestion();
    // displayQuestion(firstQuestion);
    // displayQuestionsRemaining(questions.size());
    }

    // TODO #4 add chooseNewQuestion() here
    Question chooseNewQuestion() {
      int tmpvrbl = generateRandomNumber(2);
      currentQuestionIndex = tmpvrbl;
      return questions.get(currentQuestionIndex);
      }

    // TODO #5 add getCurrentQuestion() here

    Question getCurrentQuestion() {
      return questions.get(currentQuestionIndex);
    }

    // TODO #6 add onAnswerSubmission() here
    void onAnswerSubmission() {
      Question currentPlayersQuestion = getCurrentQuestion();
      if (currentPlayersQuestion.isCorrect()) {
        totalCorrect = totalCorrect + 1;
      } else if (questions.size() == 0) {
      getGameOverMessage(totalCorrect, totalQuestions);
      startNewGame();
      } else if (questions.size() > 0) {
      chooseNewQuestion();
      questions.remove(currentQuestionIndex);
      }
      // displayQuestionsRemaining();
      // TODO: uncomment after implementing displayQuestion()
      // displayQuestion(getCurrentQuestion());
    }

    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!";
        }
    }
}

When I run the code from main class the game starts correctly, but then I add MainActivity.onAnswerSubmission(); line to Main method as the instruction says and then I get the error.

When I run the code from main class the game starts correctly, but then I add MainActivity.onAnswerSubmission(); line to Main method as the instruction says and then I get the error.

Are you typing MainActivity.onAnswerSubmission(); or mainActivity.onAnswerSubmission();… that could be your error.

Haha, you are right! I copied MainActivity.onAnswerSubmission(); from instruction, but I had to change it to mainActivity.onAnswerSubmission(); ! Thank you!

1 Like

Sweet, glad it wasn’t too bad.