Java Arrays and Loops Game Logic Pt. II

Hello . I stuck with that code. Could you help me. I get

	at MainActivity.chooseNewQuestion(MainActivity.java:32)
	at MainActivity.<init>(MainActivity.java:25)
	at Main.main(Main.java:3)

import java.util.ArrayList;

public class MainActivity {

    // TODO #1: add integer member variables here
public int currentQuestionIndex;
public int totalCorrect;
public int totalQuestions;
    // TODO #2: add ArrayList member variable here
ArrayList<Question> questions;
    // TODO #3 add startNewGame() here    
public void startNewGame() {
  
  Question question1 = new Question(921238, "How tall is the Eiffel tower?", "1024 ft", "1034 ft", "1064 ft", "1124 ft", 1);
 Question question2 = new Question(107343, "Who invented the computer algorithm?", "Charles Babbage", "John Carmack", "Alan Turing", "Ada Lovelace", 3);
 Question question3 = 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<Question>();
questions.add(question1);
questions.add(question2);
questions.add(question3);
totalCorrect = 0;
totalQuestions = 0; 
}
Question firstQuestion = chooseNewQuestion();
// displayQuestion(firstQuestion);
// displayQuestionsRemaining(questions.size());


    // TODO #4 add chooseNewQuestion() here
 public Question chooseNewQuestion(){
      int randomQuestion = generateRandomNumber(questions.size()-1);
      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(){
      Question currentQues = getCurrentQuestion();
      if (currentQues.isCorrect()){
        totalCorrect += 1;
        questions.remove(currentQuestionIndex);
        
       if(questions.size() == 0){
         System.out.println("game over");
         startNewGame();
          
        } else{
          chooseNewQuestion();
        }
      
      }
}
    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!";
        }
    }
}

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

What’s the error message?

at MainActivity.chooseNewQuestion(MainActivity.java:32)
at MainActivity.(MainActivity.java:25)
at Main.main(Main.java:3)

This only returns the location of the error, not the nature of it. The compiler is very friendly in telling us what is going wrong.

In Java Arrays and Loops Game Logic Pt. II we have 3 tabs.
One

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

Second. This is my code.

import java.util.ArrayList;

public class MainActivity {

    // TODO #1: add integer member variables here
public int currentQuestionIndex;
public int totalCorrect;
public int totalQuestions;
    // TODO #2: add ArrayList member variable here
ArrayList<Question> questions;
    // TODO #3 add startNewGame() here    
public void startNewGame() {
  
  Question question1 = new Question(921238, "How tall is the Eiffel tower?", "1024 ft", "1034 ft", "1064 ft", "1124 ft", 1);
 Question question2 = new Question(107343, "Who invented the computer algorithm?", "Charles Babbage", "John Carmack", "Alan Turing", "Ada Lovelace", 3);
 Question question3 = 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<Question>();
questions.add(question1);
questions.add(question2);
questions.add(question3);
totalCorrect = 0;
totalQuestions = 0; 

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


    // TODO #4 add chooseNewQuestion() here
 public Question chooseNewQuestion(){
  int randomQuestion = generateRandomNumber(questions.size()-1);
      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(){
      Question currentQues = getCurrentQuestion();
      if (currentQues.isCorrect()){
        totalCorrect += 1;
        questions.remove(currentQuestionIndex);
        
       if(questions.size() == 0){
         System.out.println("game over");
         startNewGame();
          
        } else{
          chooseNewQuestion();
        }
      
      }
}
    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!";
        }
    }
}

Third (How i understand it is Android code

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

Question is
Try it out!

If you’ve completed every step, Unquote is now… partially functional!

Tab over to Main.java to “play” the game:

  1. Start the game by invoking MainActivity.startNewGame();
  2. Retrieve the current Question by calling MainActivity.getCurrentQuestion();
  3. Print the Question and answers using the provided printQuestion() method
  4. Set an answer directly on the Question by modifying the playerAnswer member variable
  5. Then submit an answer by invoking MainActivity.onAnswerSubmission();
  6. Go nuts!
    .
    I dont know where to find error message?

I totaly stuck:roll_eyes:

Did you receive the message "Exception in thread “main” java.lang.NullPointerException
at MainActivity.chooseNewQuestion(MainActivity.java:38)
at MainActivity.startNewGame(MainActivity.java:29)
at Main.main(Main.java:4)?

1 Like

Now i have new problem

Main.java:9: error: non-static method getCurrentQuestion() cannot be referenced from a static context
        MainActivity.getCurrentQuestion();
                    ^
1 error

My 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;
    // TODO #3 add startNewGame() here    
public void startNewGame(){
  Question question1 = new Question(921238, "How tall is the Eiffel tower?", "1024 ft", "1034 ft", "1064 ft", "1124 ft", 1);
 Question question2 = new Question(107343, "Who invented the computer algorithm?", "Charles Babbage", "John Carmack", "Alan Turing", "Ada Lovelace", 3);
 Question question3 = 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<Question>();
questions.add(question1);
questions.add(question2);
questions.add(question3);
totalCorrect = 0;
totalQuestions = 0;
Question firstQuestion = chooseNewQuestion();
// displayQuestion(firstQuestion);
// displayQuestionsRemaining(questions.size());
}
    // TODO #4 add chooseNewQuestion() here
public Question chooseNewQuestion(){
int randomQuestion = generateRandomNumber(questions.size()-1);
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(){
      Question currentQues = getCurrentQuestion();

      if (currentQues.isCorrect()){
        totalCorrect += 1;
        questions.remove(currentQuestionIndex);
   
     if(questions.size() == 0){
         System.out.println("game over");
         startNewGame();
      
        } else{
          chooseNewQuestion();
        }
      }
}
    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!";
        }
    }
}

AAAAAAAA, whats wrong

Java needs to be updated here on this site. It will throw errors if you use var as a keyword. I’m also very disappointed with this project. It’s very verbose, intentionally confusing, and not functional. It also has NOTHING to do with Android apps.

The error comes about because you declare ArrayList for questions again within the startNewGame method, which will conflict with the questions ArrayList in the Question class. Try this inside startNewGame method:

questions = new ArrayList<Question>();