views:

250

answers:

1

The following works:

public Date getDate() {
    return date;
}

and produces something like:

<date>2010-03-23T17:43:50.879Z</date>

And I can use an XmlGregorianCalendar to return xsd:date or xsd:time types fine:

@XmlSchemaType(name="date")
public XmlGregorianCalendar getDate() {
    return date;
}

which produces something like:

<date>2010-03-23</date>

But trying to return xsd:datetime like so:

@XmlSchemaType(name="datetime")
public XmlGregorianCalendar getDate() {
     return date;
}

Causes this stack trace:

java.lang.NullPointerException
    at com.sun.xml.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl.checkXmlGregorianCalendarFieldRef(RuntimeBuiltinLeafInfoImpl.java:864)
    at com.sun.xml.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl.access$200(RuntimeBuiltinLeafInfoImpl.java:111)
    at com.sun.xml.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl$13.print(RuntimeBuiltinLeafInfoImpl.java:536)
...snip...
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)

Which doesn't mention my code at all.

I'm using tomcat 6.0.24, java 1.6.0_16-b01

+1  A: 

Well I found the basic problem: It should be "dateTime" not "datetime", however there are a number of deeper aspects to this.

First, the trace gave me very little in the way of help, and it took me a while to find the right source (jaxb-ri-2_2.src.zip). Line 864 is this:

int bitField = xmlGregorianCalendarFieldRef.get(type);

and xmlGregorianCalendarFieldRef is defined as:

private static final Map<QName, Integer> xmlGregorianCalendarFieldRef =
            new HashMap<QName, Integer>();
    static {
            Map<QName, Integer> f = xmlGregorianCalendarFieldRef;
            f.put(DatatypeConstants.DATETIME,   0x1111111);
            f.put(DatatypeConstants.DATE,       0x1111000);
            f.put(DatatypeConstants.TIME,       0x1000111);
            f.put(DatatypeConstants.GDAY,       0x1001000);
            f.put(DatatypeConstants.GMONTH,     0x1010000);
            f.put(DatatypeConstants.GYEAR,      0x1100000);
            f.put(DatatypeConstants.GYEARMONTH, 0x1110000);
            f.put(DatatypeConstants.GMONTHDAY,  0x1011000);
    }

So xmlGregorianCalendarFieldRef can't itself be null, but if get() is called with a key which isn't present in the map, it will return null, and the NullPointerException will be thrown when the JVM tries to unbox it.

This code is a bit sloppy; the type value isn't checked, which is why we get this rather cryptic message.

Obviously I then looked up the value of DatatypeConstants.DATETIME and found the case error.

It also leads me to believe I should probably use something like:

@XmlSchemaType(name=DatatypeConstants.DATETIME)
public XmlGregorianCalendar getDate() {
    return date;
}

For added protection, but that constant is a QName not a string.

Draemon