NullPointerException while trying to serialize a null value

30 views
Skip to first unread message

tomaz....@gmail.com

unread,
Sep 12, 2020, 5:46:39 AM9/12/20
to XStream User
Hi,
I noticed my app crashing in one specific case, when I pass 'null' value to convertAnother() method.

To explain it:
I override marshal() method in my custom converter and convert all fields of a certain objects. I serialize fields like this:

writer.startNode("myFieldName");
context.convertAnother(myObject);
writer.endNode();

In case myObject is null, the thing crashes in TreeMarshaller.java:48, which is the following line:
converter = converterLookup.lookupConverterForType(item.getClass());

Obviously the 'item' here is null, so calling getClass() on null crashes it. It should first verify if it is null and then use NullConverter. This is a simple fix.

My question is: can I report this as a bug or am I doing something wrong myself?

Jörg Schaible

unread,
Sep 12, 2020, 6:55:00 PM9/12/20
to xstrea...@googlegroups.com
Hi Tomaz,
The main reason for this is, that all generic converters in XStream won't
generate an XML item for null members at all. Main reason is, that the empty
value of an XML element might represent a valid value of the Java element that
is not distinguishable from null.

See, if "myFieldName" is of type String, how would you distinguish it?

Regards,
Jörg


Tomaž Kunaver

unread,
Feb 9, 2021, 6:33:53 AM2/9/21
to xstrea...@googlegroups.com
Why not put an e.g. <null/> element in that case, to distinguish it from an empty string?

--
You received this message because you are subscribed to a topic in the Google Groups "XStream User" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/xstream-user/mpzO8-uxrzA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to xstream-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xstream-user/1865684.zn4E2IaBzl%40floh.

Jörg Schaible

unread,
Feb 9, 2021, 4:31:58 PM2/9/21
to xstrea...@googlegroups.com
Because the element's name maps the class member?

Tomaž Kunaver

unread,
Mar 10, 2021, 4:32:28 AM3/10/21
to xstrea...@googlegroups.com
I was thinking of maybe nesting <null/> within <myFieldName>. In java, null is a reserved word and can't be used as a name for any field, so it can't map to class members. You would need to escape <null/> anyway if you'd want to represent it as a string. I just don't see the problem. Couldn't a logic be added to xstream that would handle null values separately?

Reply all
Reply to author
Forward
0 new messages