I am testing out the new 0.7RC1 release of Betwixt and found the following
issue when trying to write a bean to XML. The name property on the bean is
left empty and both the attributesForPrimitives and mapIDs flags are set to
true for the BeanWriter. Here is the exception (test case included below):
java.lang.NullPointerException
at
org.apache.commons.betwixt.io.AbstractBeanWriter$ElementAttributes.getURI(AbstractBeanWriter.java:1299)
at
org.apache.commons.betwixt.io.AbstractBeanWriter$IDElementAttributes.getURI(AbstractBeanWriter.java:1460)
at
org.apache.commons.betwixt.io.AbstractBeanWriter.addNamespaceDeclarations(AbstractBeanWriter.java:759)
at
org.apache.commons.betwixt.io.AbstractBeanWriter.writeElement(AbstractBeanWriter.java:813)
at
org.apache.commons.betwixt.io.AbstractBeanWriter.writeBean(AbstractBeanWriter.java:362)
at
org.apache.commons.betwixt.io.AbstractBeanWriter.writeBean(AbstractBeanWriter.java:243)
at
org.apache.commons.betwixt.io.AbstractBeanWriter.write(AbstractBeanWriter.java:174)
at random.PlainBean.main(PlainBean.java:32)
The NullPointerException occurs because the value of attributes[index] happens
to be null and the getURI() method does not check for this condition. After
doing a little debugging, it looks like a null is getting in this array because
the default ValueSuppressionStrategy filters out empty property (as in this
test) in the AbstractBeanWriter$ElementAttributes.init() method, but the
attributes array is initialized before the filtering so it contains null
entries for every filtered attribute.
The actual bean graph I am trying to persist to XML does have cyclic references
so that is why I need to set mapIDs to true. I tried setting
attributesForPrimitives to false and then the test works. I also tested this
with the 0.6 release and it seems to work.
Thanks,
glenn
---------------------------------
package random;
import java.io.StringWriter;
import org.apache.commons.betwixt.io.BeanWriter;
public class PlainBean {
private String name = null;
public PlainBean() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
PlainBean bean = new PlainBean();
// do not set name property
StringWriter sw = new StringWriter();
BeanWriter beanWriter = new BeanWriter(sw);
beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
beanWriter.getBindingConfiguration().setMapIDs(true);
beanWriter.enablePrettyPrint();
try {
beanWriter.write("bean", bean);
} catch (Exception e) {
e.printStackTrace();
// NullPointerException at AbstractBeanWriter.java:1299
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]