gpt4 book ai didi

java - 获取 NullPointerException 处理由 JAXB 解码的对象模型

转载 作者:行者123 更新时间:2023-12-02 05:09:54 25 4
gpt4 key购买 nike

UnMarshalling.java

            try { 
JAXBContext jc = JAXBContext.newInstance(QuestionList.class);
Unmarshaller um = jc.createUnmarshaller();
File file = new File("question.xml");
QuestionList que= (QuestionList) um.unmarshal(file);
List<Question> quesList=que.getQuesList();
for(Question q:quesList)
{
System.out.println(q.getId()+q.getQuestionname()+q.getComplexity());
System.out.println("Answers:");
List<Answer> list=q.getAnswers();
for(Answer ans:list)
System.out.println(ans.getId()+ans.getAnswername()+ans.getPostedby));
}
} catch (JAXBException e) {
e.printStackTrace();
}

问题.xml

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<questionList id="1">

<question>
<answers>
<answername>java is a programming language</answername>
<id>1</id>
<postedby>kristen</postedby>
</answers>

<answers>
<answername>java is a platform</answername>
<id>2</id>
<postedby>john</postedby>
</answers>

<questionname>What is java?</questionname>

<complexity>Level3</complexity>

<id>3</id>

</question>

<question>

<answers>
<answername>java is a Robust language</answername>
<id>4</id>
<postedby>sun</postedby>
</answers>

<answers>
<answername>java is a Portable</answername>
<id>5</id>
<postedby>moon</postedby>
</answers>

<questionname>Why java is popular?</questionname>

<complexity>intrado</complexity>

<id>6</id>

</question>

</questionList>

QuestionList.java

    @XmlRootElement
public class QuestionList {

private int id;
private List<Question> quesList;
public QuestionList(int id, List<Question> quesList) {
super();
this.id = id;
this.quesList = quesList;
}

public QuestionList() { }

@XmlAttribute
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

@XmlElement
public List<Question> getQuesList() {
return quesList;
}

public void setQuesList(List<Question> quesList) {
this.quesList = quesList;
}
}

问题.java

public class Question {  

private int id;
private String questionname;
private List<Answer> answers;
private String complexity;

public Question() {}


public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

public String getQuestionname() {
return questionname;
}
public void setQuestionname(String questionname) {
this.questionname = questionname;
}

public List<Answer> getAnswers() {
return answers;
}
public void setAnswers(List<Answer> answers) {
this.answers = answers;
}

public String getComplexity() {
return complexity;
}

public void setComplexity(String complexity) {
this.complexity = complexity;
}

public Question(int id, String questionname, List<Answer> answers,
String complexity) {
super();
this.id = id;
this.questionname = questionname;
this.answers = answers;
this.complexity = complexity;
}



}

Answer.java

public class Answer {  

private int id;
private String answername;
private String postedby;

public Answer() {}

public Answer(int id, String answername, String postedby) {
super();
this.id = id;
this.answername = answername;
this.postedby = postedby;
}

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAnswername() {
return answername;
}
public void setAnswername(String answername) {
this.answername = answername;
}
public String getPostedby() {
return postedby;
}
public void setPostedby(String postedby) {
this.postedby = postedby;
}
}

我收到NullPointerException,我已经发布了我的代码

Exception in thread "main" java.lang.NullPointerException
at convertXmlToJava.UnMarshalling.main(UnMarshalling.java:28)

最佳答案

Name quesList元素至question ,如QuestionList

    @XmlElement(name="question")
public List<Question> getQuesList() {
return quesList;
}

关于java - 获取 NullPointerException 处理由 JAXB 解码的对象模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27424921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com