获取由JAXB解组的NullPointerException处理对象模型

时间:2014-12-11 14:14:13

标签: java xml jaxb

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

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

Question.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)

1 个答案:

答案 0 :(得分:2)

NamequesList元素添加到question,例如QuestionList

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