Error to convert xml file to Java object with JAXB

683 views Asked by At

The xml file that I'm trying to convert is the following:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <question id="1">
        <answer>
            <answername>java is a programming language</answername>
            <id>101</id>
            <postedby>ravi</postedby>
        </answer>
        <answer>
            <answername>java is a platform</answername>
            <id>102</id>
            <postedby>john</postedby>
        </answer>
        <questionname>What is java?</questionname>
    </question>

The object I am trying to convert the file to is the following:

import java.util.ArrayList;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Question {

private int id;
private ArrayList<Answer> answers;
private String questionname;

public Question() {
}

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

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

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

public ArrayList<Answer> getAnswers() {
    return answers;
}

public void setAnswers(ArrayList<Answer> answers) {
    this.answers = answers;
}

public String getQuestionname() {
    return questionname;
}

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

}

The class of the Answer object is as follows:

public class Answer {

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

public Answer() {
}

public Answer(int id, String answername, String postedby) {
    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;
}

}

To do the conversion I created the following class:

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class App {

public static void main(String[] args) {
    File file = new File("question.xml");
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(Question.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Question question = (Question) jaxbUnmarshaller.unmarshal(file);
        
        
        System.out.println(question.getAnswers().get(0).getAnswername());
        
        
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

When I run the main method, I encounter the following exception:

Exception in thread "main" java.lang.NullPointerException at App.main(App.java:17)

Line 17 refers to: System.out.println(question.getAnswers().get(0).getAnswername());

Summary and consultation: I took my code from the following tutorial: [enter link description here][1] . I followed all the steps without skipping anything so i dont think there are mistakes in the code. The IDE I am using is Eclipse Photon with JavaSE-1.8 for my project. I did not explicitly import any libraries, and I don't use Maven. Eclipse had no trouble recognizing the annotations. So i wonder what's wrong. Can anyone help me figure it out? [1]: https://www.youtube.com/watch?v=jB_3r_Cz9Ro&t=812s

0

There are 0 answers