XStream

NullPointerException serializing an implicit list with NULL elements

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.2
  • Fix Version/s: 1.3.1
  • Component/s: Converters
  • Labels:
    None
  • JDK version and platform:
    Sun 1.5.0_15 for Windows

Description

While create xml of an object which have a list contains NULL-elements a NullPointerException is thrown.
Stacktrace:

java.lang.NullPointerException
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:93)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.visit(AbstractReflectionConverter.java:75)
at com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider.visitSerializableFields(PureJavaReflectionProvider.java:109)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:66)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:41)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:59)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:50)
at com.thoughtworks.xstream.core.TreeMarshaller.start(TreeMarshaller.java:73)
at com.thoughtworks.xstream.core.ReferenceByXPathMarshallingStrategy.marshal(ReferenceByXPathMarshallingStrategy.java:34)
at com.thoughtworks.xstream.XStream.marshal(XStream.java:745)
at com.thoughtworks.xstream.XStream.marshal(XStream.java:734)
at com.thoughtworks.xstream.XStream.toXML(XStream.java:715)
at com.thoughtworks.xstream.XStream.toXML(XStream.java:705)

Activity

Hide
Joerg Schaible added a comment -

Unless you provide a test case, all I can do is to close this issue as not reproducable.

Show
Joerg Schaible added a comment - Unless you provide a test case, all I can do is to close this issue as not reproducable.
Hide
Ralf Thielow added a comment -

Hello, here you get an example to reproduce the problem.

public class TestClass {

private List<String> items = new ArrayList();

public void setItems(List<String> items) {
this.items=items;
}

public void addItem(String it) {
items.add(it);
}

public List<String> getItems() { return this.items; } }

....

public void saveTestClass(TestClass testClass) { TestClass t = new TestClass(); t.addItem("test"); t.addItem(null); t.addItem("test2"); XStream xStream = new XStream(); xStream.toXML(t); }

...

this is principial the way you can reproduce the NPE.

Show
Ralf Thielow added a comment - Hello, here you get an example to reproduce the problem. public class TestClass { private List<String> items = new ArrayList(); public void setItems(List<String> items) { this.items=items; } public void addItem(String it) { items.add(it); } public List<String> getItems() { return this.items; } } .... public void saveTestClass(TestClass testClass) { TestClass t = new TestClass(); t.addItem("test"); t.addItem(null); t.addItem("test2"); XStream xStream = new XStream(); xStream.toXML(t); } ... this is principial the way you can reproduce the NPE.
Hide
Joerg Schaible added a comment -

... which produces

<TestClass>
  <items>
    <string>test</string>
    <null/>
    <string>test2</string>
  </items>
</TestClass>

A test for marshalling a null value is part of the acceptance tests for years (even for XStream 1.2). Any reason why you do not use the latest version? However, I have still to ask for a real test case.

Show
Joerg Schaible added a comment - ... which produces
<TestClass>
  <items>
    <string>test</string>
    <null/>
    <string>test2</string>
  </items>
</TestClass>
A test for marshalling a null value is part of the acceptance tests for years (even for XStream 1.2). Any reason why you do not use the latest version? However, I have still to ask for a real test case.
Hide
Joerg Schaible added a comment -

Without further information or a real test case, we cannot do anything.

Show
Joerg Schaible added a comment - Without further information or a real test case, we cannot do anything.
Hide
Ralf Thielow added a comment -

Try the follow class and run the test case, you got an NPE.

public class Address implements Serializable
{

/**

  • The address lines.
    */
    private List<String> addressLines;

/**

  • Returns the address lines.
  • @return address lines
    */
    public List<String> getAddressLines() { return addressLines; }

public void setAddressLines(List<String> addressLines)

{ this.addressLines = addressLines; }

/**

  • Adds an address line.
  • @param line address line
    */
    public void addAddressLine(String line) { if ( addressLines == null ) addressLines = new ArrayList<String>(); addressLines.add( line ); }

public String toXml() throws Exception

{ XStream xStream=new XStream(); xStream.addImplicitCollection(Address.class, "addressLines", "addressLine", String.class); return xStream.toXML( this ); }

}

Test case:
public void testWithXStream() throws Exception { Address address = new Address(); address.addAddressLine("test"); address.addAddressLine(null); address.toXml(); }

StackTrace:

java.lang.NullPointerException
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:93)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.visit(AbstractReflectionConverter.java:75)
at com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider.visitSerializableFields(PureJavaReflectionProvider.java:109)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:66)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:41)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:59)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:50)
at com.thoughtworks.xstream.core.TreeMarshaller.start(TreeMarshaller.java:73)
at com.thoughtworks.xstream.core.ReferenceByXPathMarshallingStrategy.marshal(ReferenceByXPathMarshallingStrategy.java:34)
at com.thoughtworks.xstream.XStream.marshal(XStream.java:745)
at com.thoughtworks.xstream.XStream.marshal(XStream.java:734)
at com.thoughtworks.xstream.XStream.toXML(XStream.java:715)
at com.thoughtworks.xstream.XStream.toXML(XStream.java:705)
at de.xcom.pitase.communication.common.Address.toXml(Address.java:39)
at de.xcom.pitase.communication.common.AddressTest.testWithXStream(AddressTest.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Show
Ralf Thielow added a comment - Try the follow class and run the test case, you got an NPE. public class Address implements Serializable { /**
  • The address lines. */ private List<String> addressLines;
/**
  • Returns the address lines.
  • @return address lines */ public List<String> getAddressLines() { return addressLines; }
public void setAddressLines(List<String> addressLines) { this.addressLines = addressLines; } /**
  • Adds an address line.
  • @param line address line */ public void addAddressLine(String line) { if ( addressLines == null ) addressLines = new ArrayList<String>(); addressLines.add( line ); }
public String toXml() throws Exception { XStream xStream=new XStream(); xStream.addImplicitCollection(Address.class, "addressLines", "addressLine", String.class); return xStream.toXML( this ); } } Test case: public void testWithXStream() throws Exception { Address address = new Address(); address.addAddressLine("test"); address.addAddressLine(null); address.toXml(); } StackTrace: java.lang.NullPointerException at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:93) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.visit(AbstractReflectionConverter.java:75) at com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider.visitSerializableFields(PureJavaReflectionProvider.java:109) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:66) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:41) at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:59) at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:50) at com.thoughtworks.xstream.core.TreeMarshaller.start(TreeMarshaller.java:73) at com.thoughtworks.xstream.core.ReferenceByXPathMarshallingStrategy.marshal(ReferenceByXPathMarshallingStrategy.java:34) at com.thoughtworks.xstream.XStream.marshal(XStream.java:745) at com.thoughtworks.xstream.XStream.marshal(XStream.java:734) at com.thoughtworks.xstream.XStream.toXML(XStream.java:715) at com.thoughtworks.xstream.XStream.toXML(XStream.java:705) at de.xcom.pitase.communication.common.Address.toXml(Address.java:39) at de.xcom.pitase.communication.common.AddressTest.testWithXStream(AddressTest.java:48) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at junit.framework.TestCase.runTest(TestCase.java:154) at junit.framework.TestCase.runBare(TestCase.java:127) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:118) at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Hide
Joerg Schaible added a comment -

Confirmed for implicit lists.

Show
Joerg Schaible added a comment - Confirmed for implicit lists.
Hide
Joerg Schaible added a comment -

Fixed in head revision.

Show
Joerg Schaible added a comment - Fixed in head revision.
Hide
Joerg Schaible added a comment -

Set correct fix version.

Show
Joerg Schaible added a comment - Set correct fix version.
Hide
Joerg Schaible added a comment -

Fixed for upcoming release.

Show
Joerg Schaible added a comment - Fixed for upcoming release.

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: