Android Development in Java | Cumulative Project 4 | Game Logic Pt. || - Debugging Help

Hi everyone,

I require some help with debugging the following pieces of code for the Android Development in Java course - Cumulative Project 4 (Lesson Link). I have completed all the tasks apart from the last one (testing the code) and I can’t seem to fix this error:
Exception in thread “main” java.lang.NullPointerException
at MainActivity.getCurrentQuestion(MainActivity.java:42)
at Main.main(Main.java:6)

Does anyone know what is wrong with the code and can explain why this is a problem?

Here is my code:

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

MainActivity.java:

import java.util.ArrayList;

public class MainActivity {

    // TODO #1: add integer member variables here
    int currentQuestionIndex;
    int totalCorrect;
    int totalQuestions;
    int randomNo;
    // TODO #2: add ArrayList member variable here
    ArrayList<Question> questions;
    // TODO #3 add startNewGame() here
    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> questions = new ArrayList<>();
      questions.add(questionOne);
      questions.add(questionTwo);
      questions.add(questionThree);

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

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

    // TODO #4 add chooseNewQuestion() here

    public Question chooseNewQuestion(){
      int randomNo = generateRandomNumber(2);
      currentQuestionIndex = randomNo;
      return questions.get(currentQuestionIndex);
      
    }

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

    // TODO #6 add onAnswerSubmission() here
    void onAnswerSubmissions(){
      getCurrentQuestion();
      if (getCurrentQuestion().isCorrect()){
        totalCorrect += 1;
      }
      questions.remove(currentQuestionIndex);
      //displayQuestionsRemaining(questions.size());
      //displayQuestionsRemaining();
      if (questions.size() == 0){
        System.out.println(getGameOverMessage(totalCorrect, totalQuestions));
        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);
    }
}

I would really appreciate any help on this.
Thank you :smile:

I guess you could check whether the currentQuestionIndex is valid (meaning less than totalQuestions).
Or you could adjust the argument for the .get in the getCurrentQuestion function.

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

Thank you so much for replying, I’ve managed to correct the error (it was a problem with the ArrayList).

Thanks again! :smile: