XML : JAXB using MOXY: NPE using @XmlPath

I am trying to generate an XML file that looks like this:

  <?xml version="1.0" encoding="UTF-8" standalone="no"?>  <Configuration technology="TLS">  <Parameters>    <Parameter name="trustStorePwd">password</Parameter>    <Parameter name="protocol">TLS</Parameter>    <Parameter name="trustStoreFile">./files/cts.bks</Parameter>    <Parameter name="port">1234</Parameter>    <Parameter name="host">localhost</Parameter>  </Parameters>  </Configuration>    

I have created the following class, with method getXML(), to create such XML: package Connection;

  import java.io.StringWriter;    import javax.xml.bind.JAXBContext;  import javax.xml.bind.JAXBException;  import javax.xml.bind.Marshaller;  import javax.xml.bind.annotation.XmlAttribute;  import javax.xml.bind.annotation.XmlElement;  import javax.xml.bind.annotation.XmlRootElement;    import org.eclipse.persistence.oxm.annotations.XmlPath;    @XmlRootElement(name="Configuration")  public class ConnectionParameters {        @XmlAttribute(name="technology")      private final String technology = "TLS";        @XmlElement(name="Parameters")      private Parameters parameters;          @SuppressWarnings("unused")      private ConnectionParameters() {        }        public ConnectionParameters(String host) {  //      this.host = host;          this.parameters = new Parameters(host);      }        public String getXML() {          try {              Marshaller m = JAXBContext.newInstance(ConnectionParameters.class).createMarshaller();              m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);                StringWriter writer = new StringWriter();                m.marshal(this, writer);                return writer.toString();          } catch (JAXBException e) {              e.printStackTrace();              return "Internal error while serializing connection parameters";          }      }        @XmlRootElement(name="Parameter")      private static class Parameters {          @XmlPath("Parameter[@name='trustStorePwd']")          private String trustStorePwd = "password";            @XmlPath("Parameter[@name='protocol']")          private String protocol = "TLS";            @XmlPath("Parameter[@name='trustStoreFile']")          private String trustStoreFile = "./files/cts.bks";            @XmlPath("Parameter[@name='port']")          private String port = "1234";                       @XmlPath("Parameter[@name='host']")          private String host;            private Parameters() {            }            private Parameters(String host) {              this.host = host;          }      }    }    

However, I get a MarshalException linked to a NullPointerException when calling getXML(), at line m.marshal(this, writer); with the following trace:

  javax.xml.bind.MarshalException   - with linked exception:  [java.lang.NullPointerException]      at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:732)      at Connection.ConnectionParameters.getXML(ConnectionParameters.java:42)      at Frontend.Frontend.sendToSP(Frontend.java:89)      at Frontend.Frontend.sendRequestForRegistrationToSP(Frontend.java:83)      at Frontend.Frontend.start(Frontend.java:73)      at Frontend.Frontend.<init>(Frontend.java:53)      at Main.Gateway.initiate(Gateway.java:58)      at Main.Gateway.main(Gateway.java:34)  Caused by: java.lang.NullPointerException      at org.eclipse.persistence.internal.oxm.XMLDirectMappingNodeValue.marshalSingleValue(XMLDirectMappingNodeValue.java:88)      at org.eclipse.persistence.internal.oxm.XMLDirectMappingNodeValue.marshal(XMLDirectMappingNodeValue.java:65)      at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:102)      at org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext.marshal(ObjectMarshalContext.java:59)      at org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:443)      at org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:424)      at org.eclipse.persistence.internal.oxm.XPathObjectBuilder.buildRow(XPathObjectBuilder.java:243)      at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:118)      at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:1)      at org.eclipse.persistence.internal.oxm.XMLCompositeObjectMappingNodeValue.marshalSingleValue(XMLCompositeObjectMappingNodeValue.java:260)      at org.eclipse.persistence.internal.oxm.XMLCompositeObjectMappingNodeValue.marshal(XMLCompositeObjectMappingNodeValue.java:151)      at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:102)      at org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext.marshal(ObjectMarshalContext.java:59)      at org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:443)      at org.eclipse.persistence.internal.oxm.XPathObjectBuilder.buildRow(XPathObjectBuilder.java:243)      at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:118)      at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:1)      at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:766)      at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshalStreamOrWriter(XMLMarshaller.java:1147)      at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:1098)      at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:1053)      at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:728)      ... 7 more    

I have no idea what I am doing wrong. Any help is much appreciated!

No comments:

Post a Comment