我有一個類TrueFalse,它擴展了一個類問題。屬性null當它看起來不應該
問題有一個屬性答案這是類型回答的ArrayList
protected ArrayList<Answer> answers = new ArrayList<Answer>();
TrueFalse有一個稱爲取(詢問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);
}
}
請附上完整的代碼和堆棧跟蹤 – 2014-11-23 23:14:13
stacktrace告訴你什麼? – Reimeus 2014-11-23 23:14:15
沒有足夠的信息超過一個SWAG,但這裏是我的:你可以通過在構造函數中重新聲明它來隱藏答案字段嗎?如果你這樣做,那麼你初始化本地變量而不是字段。請改善您的問題,以便我們不必製作SWAG。作爲一個方面的建議:不要直接訪問對象的字段。而是使用'setXXX(...)','getXXX()'和'addXXX(...)'公共方法。 – 2014-11-23 23:14:20