Cumulative Project 4 (unquote)

Hey everyone!
I think I did fairly well on the coding of the Game Logic Part 2…But I’m stuck trying to run the program and I’m receiving these mistakes. Can someone orient me in the right direction?

On MainActivity:
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

On main:
Exception in thread “main” java.lang.IndexOutOfBoundsException: Index 0 out-of-bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(
at java.base/jdk.internal.util.Preconditions.checkIndex(
at java.base/java.util.Objects.checkIndex(
at java.base/java.util.ArrayList.get(
at MainActivity.chooseNewQuestion(
at MainActivity.startNewGame(
at Main.main(

This is my MainActivity code:
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    

public void startNewGame(){

  Question question1 = new Question(012, "In Community, what's the name of Pierce's company?", "Hawthorne's Products", "Hawthorne inc.", "Hawthorne's Wipes", "Hawthorne and son", 2);

  Question question2 = new Question(123, "In SuperStore, what's Cheyenne's boyfriend's name?", "Bo", "Jim", "Kevin", "Max", 0);

  Question question3 = new Question(234, "In two Broke Girls, what's the name of the restaurant where Caroline and Max work?", "Williamsburg Coffee", "Williamsburg Diner", "Brooklyn Restaurant", "Han's famous table", 1);

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




  totalCorrect = 0;

  totalQuestions = question.size();

  Question firstQuestion = chooseNewQuestion();

// displayQuestion(firstQuestion);

// displayQuestionsRemaining(questions.size());


// TODO #4 add chooseNewQuestion() here

public Question chooseNewQuestion(){

int index = generateRandomNumber(questions.size());

currentQuestionIndex = index;

return questions.get(currentQuestionIndex); //QUESTION?


// TODO #5 add getCurrentQuestion() here

public Question getCurrentQuestion(){

return questions.get(currentQuestionIndex);


// TODO #6 add onAnswerSubmission() here

public void onAnswerSubmission(){

Question currentQues = getCurrentQuestion();

if (currentQues.isCorrect()){

totalCorrect = totalCorrect+1;

questions.remove(currentQuestionIndex);} //QUESTION

// displayQuestionsRemaining(questions.size());

if (questions.size() == 0){

System.out.println("Game over!");

startNewGame();} //Question

else {




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




And the code on main:

public class Main {

public static void main(String[] args) {

    MainActivity mainActivity = new MainActivity();


    System.out.println("Questions remaining: " + mainActivity.totalQuestions);

    Question currentQuestion = mainActivity.getCurrentQuestion();


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



Thanks so much!

Please format your code using the </> button for readability.

A main method is required for every Java application (however, this does not mean you need a main method for every class). Putting the Main and MainActivity classes in the same package is a way to accomplish this.

As for the second error, formatting your code would make it easier to see what’s wrong.

Welcome to the forums!


How do we put Main and MainActivity classes in the same package? I am also stuck with the same errors as guillaumest-hilaire7 described in this thread.

You can put a package declaration at the top of each source file you want to be part of the package. This is usually the best and clearest option.

However, if there are files in the same directory (folder), I believe they are part of a “default package” (this default package acts like any other package). On this point, someone please correct me if I’m wrong.

I highly recommend you read through Oracle’s Java tutorial on packages.

I tried to check this out with IntelliJ. It flags a class as having no package declaration when it is in a folder with another class.

I know that when you don’t put an access modifier on something, it defaults to package protected (only accessible from other classes in the package).

Thanks to stackoverflow there is a more complete answer. Just found this:

1 Like

This makes sense, there isn’t an explicit package declaration and the class isn’t part of a named package.

I use Eclipse and a default package is automatically generated (for .java files in the same directory) if one isn’t specified. Nice to know that you can’t import from this default package.

1 Like