Search code examples
javaandroidsimple-framework

mysterious Null Pointer Exception with Simple XML serializer.write


I'm having trouble using Simple Xml Serializer to build a Message from a complex object in my android project. The strange part is that it worked fine yesterday; the serialized object was written to a StringWriter and all was well. For some reason I now get this:

03-08 12:01:12.884: WARN/System.err(444): java.lang.NullPointerException
   at org.simpleframework.xml.core.Traverser.write(Traverser.java:225)
   at org.simpleframework.xml.core.Traverser.write(Traverser.java:208)
     at org.simpleframework.xml.core.Traverser.write(Traverser.java:186)
   at org.simpleframework.xml.core.Persister.write(Persister.java:1187)
    at org.simpleframework.xml.core.Persister.write(Persister.java:1169)
    at org.simpleframework.xml.core.Persister.write(Persister.java:1147)

It keeps blowing up on the serializer.write() line and I can't figure out why.

//xmlMsg is the object to be serialized.
Serializer serializer = new Persister();
StringWriter outMsg = null;
serializer.write(xmlMsg, outMsg);

Solution

  • From the javadoc it seems reasonable to me that the method will attempt to write to the Writer parameter, thus it shouldn't be null.