user4499364
user4499364

Reputation:

Object must have some value in its @XmlValue field

I have the following code

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "udt_TextType", propOrder = { "value" })
@XmlSeeAlso({ RoadTypeCodeTypeType.class })
public class UdtTextType {

@XmlValue
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
@XmlSchemaType(name = "normalizedString")
protected String value;

/**
 * Gets the value of the value property.
 * 
 * @return possible object is {@link String }
 * 
 */
public String getValue() {
    return value;
}

/**
 * Sets the value of the value property.
 * 
 * @param value
 *            allowed object is {@link String }
 * 
 */
public void setValue(String value) {
    this.value = value;
}

When I try to marshall this object:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "AdresseCompleteType", propOrder = { "inHouseMail", "buildingName", "buildingNumber", "blockName", "roadType",
        "streetName", "postcode", "cityName", "lineFive" })
public class AdresseCompleteType {

    @XmlElement(name = "InHouseMail")
    protected UdtTextType inHouseMail;
    @XmlElement(name = "BuildingName")
    protected UdtTextType buildingName;
    @XmlElement(name = "BuildingNumber")
    protected UdtTextType buildingNumber;
    ...

I have the following error: Object must have some value in its @XmlValue field

Why do I get this error?

com.sun.xml.internal.bind.api.AccessorException: Object must have some value in its @XmlValue field: fr.company.jaxb.UdtTextType@17f2dd8] at 
    fr.company.Service.launchXML(Service.java:303) at 
    fr.company.Service.launchZIP(Service.java:370) at 
    fr.company.MainTest.testGenerationZip(MainTest.java:17) at 
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at 
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57))

Thanks!

Upvotes: 7

Views: 2843

Answers (1)

bdoughan
bdoughan

Reputation: 148977

The following explains why you are getting the exception:

Java Model

Below is a simplified version of your model that demonstrates the problem:

UdtTextType

This class has a field annotated with @XmlValue:

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class UdtTextType {

    @XmlValue
    protected String value;

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

}

AdreseCompleteType

The important thing to note is that inHouseMail field is a type that has a mapping with @XmlValue.

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class AdreseCompleteType {

    @XmlElement(name = "InHouseMail")
    protected UdtTextType inHouseMail;

    public void setInHouseMail(UdtTextType inHouseMail) {
        this.inHouseMail = inHouseMail;
    }

}

XML Representation

Lets examine the different XML representations that can arise from the object model.

#1 - inHouseMail Property is null

AdreseCompleteType adreseCompleteType = new AdreseCompleteType();
adreseCompleteType.setInHouseMail(null);

In the XML representation the inHouseMail field is should as null my not including that element in the resulting XML.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<adreseCompleteType/>

#2 - inHouseMail Property is set and UdtTextType has a Value

UdtTextType udtTextType = new UdtTextType();
udtTextType.setValue("Hello World");

AdreseCompleteType adreseCompleteType = new AdreseCompleteType();
adreseCompleteType.setInHouseMail(udtTextType);

Since the inHouseMail field is set we get the InHouseMail element, and since its value field is populated that is output as well.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<adreseCompleteType>
    <InHouseMail>Hello World</InHouseMail>
</adreseCompleteType>

#3 - inHouseMail Property is set and UdtTextType has a Empty String Value

UdtTextType udtTextType = new UdtTextType();
udtTextType.setValue("");

AdreseCompleteType adreseCompleteType = new AdreseCompleteType();
adreseCompleteType.setInHouseMail(udtTextType);

Since the inHouseMail field is set we get the InHouseMail element, and since its value field is populated as an empty String that is output as an empty element.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<adreseCompleteType>
    <InHouseMail></InHouseMail>
</adreseCompleteType>

#4 - inHouseMail Property is set and UdtTextType has a null Value

UdtTextType udtTextType = new UdtTextType();
udtTextType.setValue(null);

AdreseCompleteType adreseCompleteType = new AdreseCompleteType();
adreseCompleteType.setInHouseMail(udtTextType);

The problem is what should this XML look like. It can't be an empty element like #3 since JAXB (and XML) does not represent null as an empty element. It also can't be represented as a missing InHouseMail element as that would conflict with scenario #1. So the following exception is thrown:

Exception in thread "main" javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: Object must have some value in its @XmlValue field: forum28411016.UdtTextType@f7ca0f5
com.sun.xml.internal.bind.api.AccessorException: Object must have some value in its @XmlValue field: forum28411016.UdtTextType@f7ca0f5]
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:311)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:236)
    at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:95)
    at forum28411016.Demo.main(Demo.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: com.sun.istack.internal.SAXException2: Object must have some value in its @XmlValue field: forum28411016.UdtTextType@f7ca0f5
com.sun.xml.internal.bind.api.AccessorException: Object must have some value in its @XmlValue field: forum28411016.UdtTextType@f7ca0f5
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:235)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:250)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:347)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:582)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:325)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:483)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:308)
    ... 8 more
Caused by: com.sun.xml.internal.bind.api.AccessorException: Object must have some value in its @XmlValue field: forum28411016.UdtTextType@f7ca0f5
    at com.sun.xml.internal.bind.v2.model.impl.RuntimeClassInfoImpl$TransducerImpl.writeLeafElement(RuntimeClassInfoImpl.java:395)
    at com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor$CompositeTransducedAccessorImpl.writeLeafElement(TransducedAccessor.java:239)
    at com.sun.xml.internal.bind.v2.runtime.property.SingleElementLeafProperty.serializeBody(SingleElementLeafProperty.java:114)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:343)
    ... 12 more

Upvotes: 4

Related Questions