我定义了一个包含特定答案对象属性的类 该类看起来像这样,并在尝试使用它的类中定义
protected class Answer {
String QuestionId = "";
String AnswerValue = "";
String Correct = "";
public String getQuestionId() {
return QuestionId;
}
public void setQuestionId(String arg) {
QuestionId = arg;
}
public String getAnswerValue() {
return AnswerValue;
}
public void setAnswerValue(String arg) {
AnswerValue = arg;
}
public String getCorrect() {
return Correct;
}
public void setCorrect(String arg) {
Correct = arg;
}
}
不确定上述是否正常 当我尝试使用该类时,我得到空指针错误 我正在使用它
ArrayList<Answer> answerList = new ArrayList<Answer>();
for(int a=0;a<answers.getLength(); a++){
Element eAnswer = (Element) answers.item(a);
Answer anAnswer = new Answer;
NodeList answer_nodes = eAnswer.getChildNodes();
for (int ian=0; ian<answer_nodes.getLength(); ian++){
Node ans_attr = answer_nodes.item(ian);
String tag_name = ans_attr.getNodeName();
if(tag_name.equalsIgnoreCase("answer")){
anAnswer.setAnswerValue(ans_attr.getTextContent());
}
}
answerList.add(anAnswer);
}
回答anAnswer =新答案;给出了编译错误 我所要做的就是创建一个答案列表,其中包含许多属性的名称值对
对此的任何指导都非常感激 - 特别是如果有更好的方法
答案 0 :(得分:3)
Answer anAnswer = new Answer();