简体   繁体   English

使用Jaxb unmarshal / marshal时,线程“ main”中的异常java.lang.NullPointerException

[英]Exception in thread “main” java.lang.NullPointerException when using Jaxb unmarshal/marshal

I'm using JAXB to unmarshal a given input Xml file into Java object and then marashal it back to Xml String. 我正在使用JAXB将给定的输入Xml文件解组到Java对象,然后将其编组回Xml String。 My Xml file looks like this: 我的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: 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: 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: Model.class:

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

main method: 主要方法:

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

    }

Exactly when it tries to retrieve the process id using d.getProcess.getId I get the java.lang.NullPointerException 确切地,当它尝试使用d.getProcess.getId检索进程ID时,我得到了java.lang.NullPointerException

You are mapping the namespace qualification incorrectly. 您错误地映射了名称空间限定。 You must not include the prefix in the element name. 您不得在元素名称中包含前缀。

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

To map the namespace qualification you can use the package level @XmlSchema annotation. 要映射名称空间限定,可以使用包级别@XmlSchema批注。

package-info.java 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;

For More Information 欲获得更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“ main”中的异常java.lang.NullPointerException - Exception in thread “main” java.lang.NullPointerException “线程“ main”中的异常java.lang.NullPointerException” - “Exception in thread ”main“ java.lang.NullPointerException” 线程“ main”中的异常java.lang.NullPointerException - Exception in thread “main” java.lang.NullPointerException 线程“ main”中的异常java.lang.NullPointerException - Exception in thread “main” java.lang.NullPointerException 线程“main”中的异常 java.lang.NullPointerException 5 - Exception in thread "main" java.lang.NullPointerException 5 , 线程“main”中的异常 java.lang.NullPointerException - ,Exception in thread "main" java.lang.NullPointerException 添加到HashMapList时,“主线程中的异常” java.lang.NullPointerException - “Exception in thread ”main" java.lang.NullPointerException when adding to a HashMapList 使用Selenium时收到“线程“ main” java.lang.NullPointerException中的异常” - Receiving “Exception in thread ”main“ java.lang.NullPointerException” when using Selenium Java:线程“main”java.lang.NullPointerException中的异常 - Java: Exception in thread “main” java.lang.NullPointerException 线程“ main”中的异常java.lang.NullPointerException(Java,Netbeans) - Exception in thread “main” java.lang.NullPointerException (Java, Netbeans)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM