Cumulative Project 4 - Unquote - Game Logic Pt. II - Null pointer exception in Main

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

Hi,
I am getting the following error in my code, I can’t seem to find out whats wrong…

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

The error is coming up because of:
Question firstQuestion = chooseNewQuestion();

I am able to store the object with this line of code:
Question firstQuestion = questions.get(currentQuestionIndex));

But I don’t understand why the object is not passing thru the chooseNewQuestion() method?

The following is the code in mainactivity.java, and after this code is the main.java code…

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;

// TODO #3 add startNewGame() here

public void startNewGame(){

  Question question1 = new Question(** intialized completely **);
  Question question2 = new Question(** intialized completely **);
  Question question3 = new Question(** intialized completely **);
  Question question4 = new Question(** intialized completely **);

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

  
  questions.add(question1);

  questions.add(question2);

  questions.add(question3);

  questions.add(question4);

  // reset totals

  totalCorrect = 0;

  totalQuestions = 0;

  Question firstQuestion = chooseNewQuestion();

  // displayQuestion(firstQuestion);

  //displayQuestionsRemaining(questions.size());

}

// TODO #4 add chooseNewQuestion() here

public Question chooseNewQuestion(){

  int localVar = generateRandomNumber(questions.size()-1); 

  currentQuestionIndex = localVar;

  return questions.get(currentQuestionIndex);

} 

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

}

}

Try testing this method and the variables/methods called inside.

public Question chooseNewQuestion(){
  int localVar = generateRandomNumber(questions.size()-1); 
  currentQuestionIndex = localVar;
  return questions.get(currentQuestionIndex);
} 

wow okay, so I declared the ArrayList questions inside a method, but I was suppose to declare it as an instance variable… thanks for the bit of direction :grinning:

No problem, glad you found it!