我有一个课程 TrueFalse ,它扩展了课程问题。
问题有一个答案属性,它是答案
类型的ArrayListprotected ArrayList<Answer> answers = new ArrayList<Answer>();
TrueFalse 有一个名为take(问题q)的方法,它尝试将答案添加到答案 ArrayList。
这是我的问题 - 答案 ArrayList抛出NullPointerException并显示为null。
public void take(Question question) {
input = new Input();
answer = new Answer();
System.out.print("Enter Option: ");
int response = input.getIntInput(question.options.size());
answer.setAnswerNumber(response);
System.out.println(question.answers); // PRINTS NULL
question.answers.add(answer);
}
有谁知道为什么?谢谢你的帮助!
编辑:
我相信这是堆栈跟踪?我很新,所以我认为这就是我们正在寻找的东西:
null
Exception in thread "main" java.lang.NullPointerException
at Question.TrueFalse.take(TrueFalse.java:53)
at Survey.Survey.take(Survey.java:239)
at Survey.Main.mainMenuSelection(Main.java:74)
at Survey.Survey.mainMenu(Survey.java:320)
at Survey.Main.mainMenuSelection(Main.java:41)
at Survey.Survey.mainMenu(Survey.java:320)
at Survey.Main.main(Main.java:196)
EDIT2:
这是调用take方法的地方,来自我的调查类。
public void take(Survey survey) {
if (survey.name == null && survey.questions.isEmpty()) {
System.out.println("MESSAGE: Please create/load first.");
mainMenu();
}
System.out.print("Name: ");
System.out.println(survey.name);
for (int i = 0; i < survey.questions.size(); i++) {
System.out.print("\n" + (i+1) + ". " + survey.questions.get(i).questionType + ") ");
System.out.println(survey.questions.get(i).prompt);
for(int j = 0; j < survey.questions.get(i).options.size(); j++) {
System.out.print(j+1 + ". ");
System.out.println(survey.questions.get(i).options.get(j));
}
survey.questions.get(i).take(survey.questions.get(i)); // CALLED HERE
}
System.out.println("Survey complete!");
}
EDIT3:
这是我的问题类
public abstract class Question implements Serializable {
protected static final long serialVersionUID = 1L;
protected Input input;
protected Answer answer;
protected int optionCount = 0;
public String prompt;
public String questionType;
public ArrayList<String> options;
public ArrayList<String> correctOptions;
public ArrayList<String> matchingOptions;
public ArrayList<Answer> answers;
public Question() {
options = new ArrayList<String>();
correctOptions = new ArrayList<String>();
matchingOptions = new ArrayList<String>();
answers = new ArrayList<Answer>();
}
注意:我不是最好的程序员,而是新手,所以它可能非常草率:(
EDIT4:
我的 TrueFalse 类:
public class TrueFalse extends Question implements Serializable {
private static final long serialVersionUID = 1L;
public TrueFalse() {
questionType = "True/False";
}
@Override
public void addPrompt() {
input = new Input();
System.out.println("Enter the prompt for your True/False question: ");
prompt = input.getStringInput();
}
@Override
public void addOption() {
options.add("True");
options.add("False");
}
@Override
public void addCorrectOption() {
System.out.println("Select correct option: T/F");
String response = input.getStringInput();
while ((!response.toLowerCase().equals("t")) && (!response.toLowerCase().equals("f"))) {
System.out.println("MESSAGE: Enter a valid input.");
}
if (response.toLowerCase().equals("t")) {
correctOptions.add("True");
} else {
correctOptions.add("False");
}
}
public void take(Question question) {
input = new Input();
answer = new Answer();
System.out.print("Enter Option: ");
int response = input.getIntInput(question.options.size());
answer.setAnswerNumber(response);
System.out.println(question.answers);
question.answers.add(answer);
}
}
答案 0 :(得分:0)
您确定在调查中正确初始化了问题对象吗?
survey.questions.get(i).take(survey.questions.get(i));
一般来说,我不会放弃确定这条线的含义是什么。对于我的调查,从我的问题中选择一个特定的问题,然后让这个问题激活一个方法......本身?这就是为什么这个关键字首先用于... ...也许你应该尝试:
public void take() {
input = new Input();
answer = new Answer();
System.out.print("Enter Option: ");
int response = input.getIntInput(this.options.size());
answer.setAnswerNumber(response);
System.out.println(this.answers); // NO NEED TO SEND THE QUESTION TO ITSELF
this.answers.add(answer);
}
答案 1 :(得分:-1)
但是,我会说使用这里的“受保护”属性对我不好看,会在问题类中推荐你:
private ArrayList<Answer> answers = new ArrayList<Answer>();
protected ArrayList<Answer> getAnswers(){
return answers;
}
在子类TrueFalse中,通过getter方法
来引用它System.out.println(question.getAnswers());
question.getAnswers().add(answer);
答案 2 :(得分:-2)
我在问题类中取出了答案ArrayList的分配,并将其放在我的take()方法中,如下:
public void take(Question question) {
input = new Input();
answer = new Answer();
answers = new ArrayList<Answer>();
System.out.print("Enter Option: ");
int response = input.getIntInput(question.options.size());
answer.setAnswerNumber(response);
System.out.println(question.answers);
question.answers.add(answer);
}
在问题中,现在看起来像是:
public ArrayList<Answer> answers;
有谁知道为什么这会解决它?