线程" main"中的例外情况使用Jaxb unmarshal / marshal时的java.lang.NullPointerException

时间:2014-04-01 10:02:22

标签: java xml jaxb marshalling unmarshalling

我正在使用JAXB将给定的输入Xml文件解组为Java对象 然后将它重新编写回Xml String。 我的Xml文件如下所示:

<bpmn2:definitions xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" id="_Definitions_1">
    <bpmn2:process id="_500441" name="process">
    </bpmn2:process>
</bpmn2:definitions>

Definitions.class:

@XmlRootElement(namespace = "http://www.omg.org/spec/BPMN/20100524/MODEL")
public class Definitions {
    @XmlAttribute
    private String id;

    @XmlElement(name = "bpmn2:process")
    private Process process;

    @XmlElement(name = "bpmndi:BPMNDiagram")
    private Diagram diagram;

    public Definitions() {
    }
    public Definitions(String id, Process process, Diagram diagram) {
        this.id = id;
        this.process = process;
        this.diagram = diagram;
    }
    public Process getProcess() {
        return process;
    }
    public Diagram getDiagram() {
        return diagram;
    }
    public String getId() {
        return id;
    }
}

Process.class:

@XmlAccessorType(XmlAccessType.FIELD)
public class Process {
    @XmlAttribute
    private String id;
    public Process() {
    }
    public Process(String id) {
        this.id = id;
    }
    public String getId() {
        return id;
    }
}

Model.class:

public class Model {
    @XmlElement
    private Process process;
    public Model() {
    }
    public Model(String processId, Process p) {
        this.id = processId;
        this.process = p;
    }
}

主要方法:

public static void main(String[] args) throws IOException, JSONException, JAXBException {
        BpmnToJsonImport bj = new BpmnToJsonImport();
        InputStream is = BpmnToJsonImport.class.getResourceAsStream("myXml.txt");
        String Str = IOUtils.toString(is);
        StringReader sr = new StringReader(Str);
        JAXBContext context = JAXBContext.newInstance(Definitions.class, Model.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        Definitions d = (Definitions) unmarshaller.unmarshal(sr);
        Model model = new Model(d.getProcess().getId(), d.getProcess());

        StringWriter sw = new StringWriter();

        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.marshal(model, sw);
        String str = sw.toString();
        System.out.println(str);

    }

当它尝试使用d.getProcess.getId检索进程id时,我得到了  的显示java.lang.NullPointerException

1 个答案:

答案 0 :(得分:1)

您正在错误地映射名称空间限定。您不得在元素名称中包含前缀。

@XmlElement(name = "BPMNDiagram")
private Diagram diagram;

要映射命名空间限定,您可以使用包级别@XmlSchema注释。

<强> package-info.java

@XmlSchema( 
    namespace =  "http://www.omg.org/spec/BPMN/20100524/MODEL",
    elementFormDefault = XmlNsForm.QUALIFIED) 
package example;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

了解更多信息