MaVVamaldo
MaVVamaldo

Reputation: 2535

@XmlElementWrapper generates a NullPointerException

hi all I am trying to represent the following xml code:

        <AggregationMeta>
            <AggregationLink>
                <LocalName>stuff</LocalName>
                <Value>stuff</Value>
                <Meta/>
            </AggregationLink>
            <AggregationLink>
                <LocalName>stuff</LocalName>
                <Value>stuff</Value>
                <Meta/>
            </AggregationLink>
        </AggregationMeta>

and I want to do this using @XmlElementWrapper annotation.

here's the class where I use it:

public class VrIdnMeta {

private String lri;
private String nodeLocalName;
private List<Link> referenceLink;
private List<Link> aggregationLink;
private List<SimpleLink> backLink;
private List<SimpleLink> incomingChangeLink;

...

@XmlElementWrapper(name = "ReferenceMeta")
@XmlElement(name = "ReferenceLink")
public List<Link> getReferenceLink() {
    return referenceLink;
}

public void setReferenceLink(List<Link> referenceLink) {
    this.referenceLink = referenceLink;
}

@XmlElementWrapper(name = "AggregationMeta")
@XmlElement(name = "AggregationLink")
public List<Link> getAggregationLink() {
    return aggregationLink;
}

public void setAggregatioinLink(List<Link> aggregationLink) {
    this.aggregationLink = aggregationLink;
}

@XmlElementWrapper(name = "BackLinkMeta")
@XmlElement(name = "BackLink")
public List<SimpleLink> getBackLink() {
    return backLink;
}

public void setBackLink(List<SimpleLink> backLink) {
    this.backLink = backLink;
}

@XmlElementWrapper(name = "IncomingChangeMeta")
@XmlElement(name = "IncomingChangeLink")
public List<SimpleLink> getIncomingChangeLink() {
    return incomingChangeLink;
}

public void setIncomingChangeLink(List<SimpleLink> incomingChangeLink) {
    this.incomingChangeLink = incomingChangeLink;
}

}

and here's the SimpleLink class

public class SimpleLink {

private String localName;
private String value;

...

@XmlElement(name = "LocalName")
public String getLocalName() {
    return localName;
}

public void setLocalName(String localName) {
    this.localName = localName;
}

@XmlElement(name = "Value")
public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}
}

and finally the Link class

public class Link extends SimpleLink{

private String meta;

public Link(String localName, String value, String meta) {
    super(localName, value);
    this.meta = meta;
}

public Link() {
    super();
}

public Link(String localName, String value) {
    super(localName, value);
}

@XmlElement(name = "Meta")
public String getMeta() {
    return meta;
}

public void setMeta(String meta) {
    this.meta = meta;
}

}

the problem comes when I use Jaxb2 to unmarshal an xml alike to the one I wrote above: I get a NullPointerException :-(. I read that there is a bug in jaxb about this fact but it was solved in 2006 and I am using spring 3 (with his own jaxb2 library).

Anyone has a clue?

Upvotes: 1

Views: 625

Answers (1)

bdoughan
bdoughan

Reputation: 149037

The NPE seems to stem from the names of your get and set methods not matching up (in VrIdnMeta):

@XmlElementWrapper(name = "AggregationMeta")
@XmlElement(name = "AggregationLink")
public List<Link> getAggregationLink() {
    return aggregationLink;
}

public void setAggregatioinLink(List<Link> aggregationLink) {
    this.aggregationLink = aggregationLink;
}

When I changed setAggregatioinLink to setAggregationLink I got your example to run as expected.

Upvotes: 1

Related Questions