Game Logic II help needed: Main method not found (& probably loads more errors)

I’m sure my code is absolutely littered with errors because I found this project very challenging, but I managed to debug most of them until I’m down to one, and I can’t seem to solve it:

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

Initially I had loads of errors down to the fact that I mixed question/Questions. I’m not entirely convinced I solved that…

Here is my code, in this project all the coding has happened in Main Activity, but I’ve included Questions.java and Main.java for extra context

MAINACTIVITY

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.   

public void startNewGame(){ 

  Question questionOne = new Question (921238, "How tall is the Eiffel Tower?","1024 ft", "1063 ft", "1124 ft", "1163 ft", 1);
  Question questionTwo = new Question (107343, "Who invented the computer algorithm?", "Charles Babbage", "John Carmack", "Alan Turing", "Ada Lovelace",3);
  Question questionThree = new Question (748294, "What is the name for the patch of skin found on your elbow?", "Elbow Skin", "Fascia Elbora", "Wenis", "Todd", 2);

ArrayList<Question> question = new ArrayList<Question>(); 

      question.add(questionOne);
      question.add(questionTwo);
      question.add(questionThree);

totalCorrect = 0;
totalQuestions = question.size();


Question firstQuestion = chooseNewQuestion();
// displayQuestion(firstQuestion);
// displayQuestionsRemaining(questions.size());
}
// TODO #4 add chooseNewQuestion() here. 
public Question chooseNewQuestion(){
    int randomNumber = generateRandomNumber(questions.size()); 
    currentQuestionIndex = randomNumber;
    return questions.get(currentQuestionIndex);

}

// TODO #5 add getCurrentQuestion() here

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

}

// TODO #6 add onAnswerSubmission() here
public void onAnswerSubmission(){
  Question currentQuestion = getCurrentQuestion();
  if (currentQuestion.isCorrect()){
    totalCorrect =+1;
  } 

  questions.remove(currentQuestion);
  // displayQuestionsRemaining(questions.size());

  if (questions.size()==0){
    System.out.println("Game over!");
    startNewGame();
  } else {
    chooseNewQuestion();
  }
}

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

MAIN.JAVA

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);
    }
}

QUESTION.JAVA

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

Any help appreciated because I’m tearing my hair out a bit here. Would be nice if a solution from codecademy was available so you could actually figure out where you went wrong - of course you don’t want to encourage people to peak at the answers, but it feels very non-productive to just have to abandon a project when you can’t figure it out! New to coding & first time posting so let me know if I’ve missed any key info :slight_smile:

Link to the project: https://www.codecademy.com/paths/introduction-to-android-with-java/tracks/java-arrays-and-loops/modules/cumulative-project-4/projects/unquote-game-logic-pt-ii

Are you compiling Main.java or MainActivity.java?

On a terminal you might type

javac Main.java //to compile
java Main.java //to execute

for the program to run. Wherever your main method is — that’s the principal file to compile.

You may still get errors, but at least the execution won’t be what the problem is. (Note, all the files have to be in the same directory, and I’m assuming they’re titled with case-sensitive titles, MainActivity.java, Question.java, Main.java.

Hi - there isn’t actually a bash terminal to do that in this exercise (is that what it’s called? The bit to the right where you compile java!). This message appears when I save on the “Question” or “MainActivity” tabs, but when I save on the “Main” tab, it does display the question as it should, so I decided to try and play it - however I really can’t figure out how to play it. These are the instructions:

Try it out!

If you’ve completed every step, Unquote is now… partially functional!

Tab over to Main.java to “play” the game:

  1. Start the game by invoking MainActivity.startNewGame();
  2. Retrieve the current Question by calling MainActivity.getCurrentQuestion();
  3. Print the Question and answers using the provided printQuestion() method
  4. Set an answer directly on the Question by modifying the playerAnswer member variable
  5. Then submit an answer by invoking MainActivity.onAnswerSubmission();
  6. Go nuts!

However when I try to invoke MainActivity.startNewGame(); where it is indicated to //Play Game in the code, I get this error:

Main.java:9: error: non-static method getCurrentQuestion() cannot be referenced from a static context
MainActivity.getCurrentQuestion();
^
I’m not sure if that’s connected to the other error, or I’ve tried to invoke in the wrong place… either way I’m totally lost with this

In this case you have to still run from Main.java (here, it would be hitting save on the Main.java file, and as a safety measure check that the other files are saved).

With your code this still yields an error, so you have to debug.

However, your MainActivity does instantiate:

public class Main {
    public static void main(String[] args) {
        MainActivity mainActivity = new MainActivity();
        System.out.println("Hello, Morld"); //correctly prints!
        //mainActivity.startNewGame();

which means the first place you should look is the startNewGame() method.

Thanks for your reply, but I’ll be honest, I’ve played around with it and I still have absolutely no idea what I have to do to start the game. The whole of Main was pre-coded by codecademy, do I have to change some of their code? Sorry, I’m so lost!

So to be clear: the errors are in MainActivity.java. But since the main invokes and runs MainActivity, it’s your principal place to start debugging.

I’ll outline the first steps:

  • Comment out everything but the first lines in the main method and test with a string:
public static void main(String[] args) {
        MainActivity mainActivity = new MainActivity();
        System.out.println("Hello, World");
        //mainActivity.startNewGame();
        //System.out.println("Questions remaining: " + mainActivity.totalQuestions);
        //Question currentQuestion = mainActivity.getCurrentQuestion();
        //printQuestion(currentQuestion);
        // Play the game!
    }

  • Uncomment out the next line:
MainActivity mainActivity = new MainActivity();
        System.out.println("Hello, World");
        mainActivity.startNewGame();
        //System.out.println("Questions remaining: " + mainActivity.totalQuestions);

You’ll probably get an error like:

Exception in thread “main” java.lang.NullPointerException
at MainActivity.chooseNewQuestion(MainActivity.java:38)
at MainActivity.startNewGame(MainActivity.java:32)
at Main.main(Main.java:4)

This logically means that there must be an issue with your startNewGame() method at the very least, even if you don’t know how to track a NullPointerException.

Ok so let’s do the same process with startNewGame()… comment out:

public void startNewGame(){ 
      Question questionOne = new Question (921238, "How tall is the Eiffel Tower?","1024 ft", "1063 ft", "1124 ft", "1163 ft", 1);
      System.out.println("test");
  /*
       Question questionTwo = new Question (107343, "Who invented the computer algorithm?", "Charles Babbage", "John Carmack", "Alan Turing", "Ada Lovelace",3);
      Question questionThree = new Question (748294, "What is the name for the patch of    skin found on your elbow?", "Elbow Skin", "Fascia Elbora", "Wenis", "Todd", 2);

      ArrayList<Question> question = new ArrayList<Question>(); 

      question.add(questionOne);
      question.add(questionTwo);
      question.add(questionThree);

      totalCorrect = 0;
      totalQuestions = question.size();


      Question firstQuestion = chooseNewQuestion();
      // displayQuestion(firstQuestion);
      // displayQuestionsRemaining(questions.size());*/
}

and go back and test by saving Main, it should give you:
Screen Shot 2022-01-25 at 10.04.22 AM

As you can see however, this crawling bit by bit is a tedious process. As you get more experience you get better with avoiding this type of situations but you have to think about testing and debugging techniques. When you write a new method or a new pattern of code you’ve never tested before, try running it first. When there are long lines of code since the last test you’ve made, definitely test. And so forth.

Thanks so much for taking the time to write this step by step advice.
I followed your instructions and went step by step to find my error (something I will take forward - really good advice!). I think I found the error, half way down main.java:

Main.java:16: error: cannot find symbol
printQuestion(currentQuestion);
^
symbol: method printQuestion(Question)
location: class Main
1 error

however, this was code that was already written by codecademy & was not part of the exercise, and there was no instructions to change it. Could it be that this isn’t the error?

I did manage to make the game work, regardless of the error I was experiencing before, so maybe the error wasn’t significant? Although I would still like to understand!