Cumulative Project 4 Don't know why my code isn't working

I’m just not too sure what’s wrong with my code, but it just doesn’t seem to be working correctly. I’m not able to change the playerAnswer so that it has any effect meaning, the onAnswerSubmission doesn’t seem to work either.

Here is the code for my

public class Question {

    int imageId;

    String questionText;

    String answer0;

    String answer1;

    String answer2;

    String answer3;

    int correctAnswer;

    int playerAnswer = 1;

    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;

    public boolean isCorrect() {

      if(correctAnswer == playerAnswer){
        return true;
      return false;

    public static void main(String[] args){


Then here is the code for the

import java.util.ArrayList;

import java.util.Arrays;

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




      totalCorrect = 0;

      totalQuestions = questions.size();

      Question firstQuestion = chooseNewQuestion();




    // TODO #4 add chooseNewQuestion() here

    public Question chooseNewQuestion(){

      int n = generateRandomNumber(2);

      currentQuestionIndex = n;


        return (Question)questions.get(n);



    // TODO #5 add getCurrentQuestion() here

    public Question getCurrentQuestion(){

      return (Question)questions.get(currentQuestionIndex);


    // TODO #6 add onAnswerSubmission() here

    public void onAnswerSubmission(){

      if(chooseNewQuestion().isCorrect() ){

        totalCorrect = totalCorrect + 1;



     // displayQuestionsRemaining(questions.size()); 

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

       System.out.println("Game Over");






     // TODO: uncomment after implementing displayQuestion()

// displayQuestion(getCurrentQuestion());


    public int generateRandomNumber(int max) {

      double randomNumber = Math.random();

      double result = 3.0 * 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){


and then here is the Main code

public class Main {

    public static void main(String[] args) {

        MainActivity mainActivity = new MainActivity();


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

        Question currentQuestion = mainActivity.chooseNewQuestion();

        // Play the game!







    static void printQuestion(Question question) {

        System.out.println("Question: " + question.questionText);

        System.out.println("Answer 1: " + question.answer0);

        System.out.println("Answer 2: " + question.answer1);

        System.out.println("Answer 3: " + question.answer2);

        System.out.println("Answer 4: " + question.answer3);



It seems to randomly print out the questions which is good, but then every now and again it will print the question and this error

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 2 out-of-bounds for length 2
	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.onAnswerSubmission(
	at Main.main(

I’m just really not sure what is going wrong.

Here is a link to the full project

Your error indicates you are trying to access an element of the ArrayList questions at index 2, but the ArrayList only has 2 elements, so the valid indices would be 0 and 1. The problem likely occurs after you’ve removed one or more questions from the ArrayList, and your random index generator returns a number that is out of bounds. Think about how you could modify the code in your generateRandomNumber() method to keep the returned value in bounds. Another observation of the method in question is that you currently accept a parameter, int max, but do not use it in the method.

Yep think I’ve fixed that. However the thing now that I can’t get to work is accessing and changing the member variable playerAnswer from the, so that it has an impact in the Main code.