Cumulative Project 4: index out of bounds <ArrayList>

I tried copy pasting the content of startNewGame() method into a main method in and i managed to print out the size of the array which was at 3. When the method is being called upon in Main.Java i think the ‘Question’ objects aren’t being moved into my questions arraylist… I might be completely off in what the problem is though.

This is the error that im getting:
“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(”

Code in Main.Java

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



Code in MainActivity.Java

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(921238,"How tall is the Eiffel tower?","1024 ft","1063 ft","1124 ft","1163 ft",1 );
      Question question2 = new Question(922235, "What's the best hand", "AA", "27o", "5s6s","AK", 1 );
      Question question3 = new Question(923123, "Whats beyond level Ten?", "Ten", "Hundred", "Eleven","Neverland", 3);
      ArrayList<Question> questions = new ArrayList<>();

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


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

    // TODO #5 add getCurrentQuestion() here
    public Question getCurrentQuestion(){
    return questions.get(currentQuestionIndex);

    // TODO #6 add onAnswerSubmission() here
    public void onAnswerSubmission(){
      /* so we should use getCurrentQuestion() to get the question we're on and then compare the int for correct answer in the object Question x to the players answer. If it is correct we modify totalcorrect to +1*/
      //since the object Question holds 5 variables, how to compare...
      //just looked at hint, seems like we've made a isCorrect method
      if (getCurrentQuestion().isCorrect()){
      // displayQuestionsRemaining(questions.size());
      if (questions.size() == 0){
        System.out.println("game over");
      // 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!";
    public static void main(String[] args){


There’s actually alot wrong with my code, found a couple and the ball is rolling again. Ill report later if i get stuck again - for now no need to check for whats wrong with the code. Couldnt delete the thread.