0

I am very confused!! In my Question class, I am trying to print a question in an array and the corresponding multiple choice answers stored in a separate 2D array. I put that in a loop and am passing int row from the main method and trying to get user input and pass that back to my Question class. Probably doesn't make sense but I get a null point error. Here's all of my code

public class Question{
    private int selectedAnswer;
    public String[] questions =
    {
        "Favourite sweet",
        "Favourite subject at Hogwarts",
        "Dream vacation",
        "Favourite Drink",
        "Dream House",
        "What do you desire the most?",
        "Favourite dress robe colour",
        "Pick a muggle career",
        "Pick a creature"
    };

private String[][] options =
{
    {"1.Acid Pops","2.Sherbert Lemons","3.Bertie Bott's Every Flavour Beans", 
    "4.Cake","5.Hagrid's Rock Cakes","6.Chocolate Frogs","7.Ginger Newt",
    "8.I hate sweets"},

    {"1.Care of Magical Creatures","2.Charms","3.Defense Against the Dark Arts",
    "4.Divination","5.Herbology","6.History of Magic","7.Muggle Studies","8.Potions",
    "9.Study of Ancient Runes","10.Transfiguration"},

    {"1.Anywhere with friends","2.Egypt","3.Hogwarts","4.Museum","5.India","6.Forest",
    "7.Can't be bothered with a vacation"},

    {"1.Unicorn blood", "2.Pumpkin Juice", "3.Butter beer", "4.Coca-Cola", "5.Tea", "6.Coffee", "7.Brandy"},

    {"1.The Burrow", "2.A Cottage", "3.Thirteen Grimmauld Place", "4.Malfoy Manor"},

    {"1.Friends", "2.Success", "3.Money", "4.Power"},

    {"1.Black", "2.Red", "3.Pink", "4.Green", "5.Orange", "6.Blue"},

    {"1.Lawyer", "2.Teacher", "3.Social Worker", "4.Prime Minister", "5.Google Employee"},

    {"1.Centaur", "2.Basilisk", "3.Unicorn", "4.Thestral", "5.Phoenix", "6.Hippogriff", "7.Dementor"}

 };
    private String[] quizQuestion;
    private String[][] quizOptions; 
//new update:
    private String quizQuestion;
    private String[] quizOptions

    public Question(int row){

      for(int i= row; i< questions.length; i++){
        **quizQuestion[i] = questions[i];** //null point error
          quizQuestion = questions[i]; // new update
        for(int j = row; j < options[i].length; j++){
            quizOptions[i][j] = options[i][j];
            quizOptions[i] = options[i][j];  // new update

            } 
         }
      }

    public String[] getQuizQuestion(){
       **return this.quizQuestion;** //null point error
    } 
    public String[][] getQuizOptions(){
      return this.quizOptions;
    }
    public void setSelectedAnswer(int userInput){
       selectedAnswer = userInput;
    }
}

Main method

import java.util.Scanner;
public class Main{
public static void main(String[] args){
    Question q = new Question(0);
    System.out.println(q.getQuizQuestion());
    System.out.println(q.getQuizOptions());

    Scanner keyboard = new Scanner (System.in);
    int userInput = keyboard.nextInt(); 
    System.out.println("Select an answer: ");
    q.setSelectedAnswer(userInput);

   }
}
LLgood
  • 37
  • 2
  • 8
  • Your `quizQuestion` and `quizOptions` are uninitialised and are set to null by default. So, you are seeing NPE. – harshavmb Mar 01 '18 at 03:03

1 Answers1

2

You have to create an array before you can fill it.

public Question(int row){
   quizQuestion = new String[ <expected length> ]; 
   quizOptions = new String[ <expected length> ][]; //Outer array

  for(int i= row; i< questions.length; i++){
    **quizQuestion[i] = questions[i];** //null point error
    quizOptions[i] = new String[]; //Each inner array
    for(int j = row; j < options[i].length; j++){
        quizOptions[i][j] = options[i][j];
        } 
     }
  }

But, as far as I can tell, quizQuestion should just be one String. And quizOptions should be a String array. If a Question object holds the details of only one question, the question should be a String and the options should be only the options of that question.

Update:

private String quizQuestion;
private String[] quizOptions

public Question(int row){
    quizQuestion = questions[row]; 
    quizOptions = new String[options[row].length]; 
    for(int j = 0; j < options[row].length; j++){
        quizOptions[j] = options[row][j];  

    } 
  }

public String getQuizQuestion(){
   return this.quizQuestion;
} 
public String[] getQuizOptions(){
  return this.quizOptions;
}

Also, you may not need to copy options at all:

private String quizQuestion;
private String[] quizOptions

public Question(int row){
    quizQuestion = questions[row]; 
    quizOptions = options[row]; //Just refer to the existing piece
  }

public String getQuizQuestion(){
   return this.quizQuestion;
} 
public String[] getQuizOptions(){
  return this.quizOptions;
}
Teddy
  • 4,009
  • 2
  • 33
  • 55
  • You're right Teddy it should be a String for quizQuestion and an array for quizOptions. So i've changed it to that. But the problem is that each multiple choice answer length for quizOptions is different. So I don't know what to initialize it to – LLgood Mar 01 '18 at 03:41
  • You can quizOptions[i] = new String[ options[i].length ]; //Each inner array – Teddy Mar 01 '18 at 03:56
  • Sorry to be a pest but I get this error now:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at Question.(Question.java:51) at Main.main(Main.java:4) from quizOptions[i] = options[i][j]; – LLgood Mar 01 '18 at 04:01
  • Pls put an "Edit" and add your current code in the question, I'll check it. – Teddy Mar 01 '18 at 04:11
  • Updated my ans. – Teddy Mar 01 '18 at 04:21
  • it wont print the selected answers just a reference. – LLgood Mar 01 '18 at 04:36