Java null point异常错误

时间:2018-03-01 02:48:07

标签: java arrays nullpointerexception

我很困惑!! 在我的Question类中,我试图在一个数组中打印一个问题,并将相应的多选答案存储在一个单独的2D数组中。我把它放在一个循环中,并从main方法传递int行并尝试获取用户输入并将其传递回我的Question类。可能没有意义,但我得到一个零点错误。 这是我的所有代码

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

主要方法

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

   }
}

1 个答案:

答案 0 :(得分:2)

您必须先创建一个数组才能填充它。

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

但是,据我所知,quizQuestion应该只是一个字符串。 quizOptions应该是一个String数组。如果一个Question对象只包含一个问题的详细信息,那么问题应该是一个字符串,选项应该只是该问题的选项。

更新

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

此外,您可能根本不需要复制选项:

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