Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 361689

Summary: NPE thrown when @XmlValue mapping attempts to write xsi:type
Product: z_Archived Reporter: Blaise Doughan <blaise.doughan>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
URL: http://stackoverflow.com/questions/7851506/jaxb-how-to-set-the-xsitype-of-a-xml-element-based-on-its-value
Whiteboard:
Attachments:
Description Flags
MOXy - Test Cases
none
Core - Fix none

Description Blaise Doughan CLA 2011-10-21 12:43:00 EDT
If you attempt to marshal an object like the following a NPE will be thrown when an xsi:type attribute needs to be written for the @XmlValue property.

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Field {
    @XmlAttribute
    private String name;

    @XmlValue
    Object value;

}

Exception

Exception in thread "main" javax.xml.bind.MarshalException
 - with linked exception:
[Exception [EclipseLink-25003] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred marshalling the object
Internal Exception: java.lang.NullPointerException]
	at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:314)
	at forum7851506.Demo.main(Demo.java:20)
Caused by: Exception [EclipseLink-25003] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred marshalling the object
Internal Exception: java.lang.NullPointerException
	at org.eclipse.persistence.exceptions.XMLMarshalException.marshalException(XMLMarshalException.java:78)
	at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:516)
	at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:312)
	... 1 more
Caused by: java.lang.NullPointerException
	at org.eclipse.persistence.oxm.record.OutputStreamRecord.outputStreamWrite(OutputStreamRecord.java:511)
	at org.eclipse.persistence.oxm.record.FormattedOutputStreamRecord.openStartElement(FormattedOutputStreamRecord.java:84)
	at org.eclipse.persistence.internal.oxm.XPathNode.startElement(XPathNode.java:330)
	at org.eclipse.persistence.internal.oxm.XMLCompositeObjectMappingNodeValue.marshalSingleValue(XMLCompositeObjectMappingNodeValue.java:185)
	at org.eclipse.persistence.internal.oxm.XMLCompositeObjectMappingNodeValue.marshal(XMLCompositeObjectMappingNodeValue.java:116)
	at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:104)
	at org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext.marshal(ObjectMarshalContext.java:60)
	at org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:322)
	at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:325)
	at org.eclipse.persistence.internal.oxm.XMLCompositeObjectMappingNodeValue.marshalSingleValue(XMLCompositeObjectMappingNodeValue.java:176)
	at org.eclipse.persistence.internal.oxm.XMLCompositeObjectMappingNodeValue.marshal(XMLCompositeObjectMappingNodeValue.java:116)
	at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:104)
	at org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext.marshal(ObjectMarshalContext.java:60)
	at org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:322)
	at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:325)
	at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:932)
	at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:486)
	... 2 more
Comment 1 Blaise Doughan CLA 2012-03-15 16:00:45 EDT
Created attachment 212752 [details]
MOXy - Test Cases
Comment 2 Blaise Doughan CLA 2012-03-15 16:01:16 EDT
Created attachment 212753 [details]
Core - Fix
Comment 3 Blaise Doughan CLA 2012-03-16 12:31:35 EDT
Fix checked into 2.3.3 at rev:  11004
Fix checked into trunk at rev:  11005

Code reviewed by:  Matt MacIvor

Fix Details:
When writing out text in XMLCompositeObjectMappingNode value treat the XPath "@text" like ".".
Comment 4 Eclipse Webmaster CLA 2022-06-09 10:26:11 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink