Discussion:
Xstream difference in behaviour between android 4.1 and 4.3
Charles Colbourn
2014-09-09 15:50:12 UTC
Permalink
Hi

I've stumbled over a bit of an oddity with Xstream across these two versions
of Android.

If you have an annotated field:

@XStreamImplicit
public ArrayList<String> myMessages = new ArrayList<String>();

and you read XML using Xstream.fromXML(string);

and test to see if your field has been populated:

if ( myMessages.size()>0 ) {#dostuff


On 4.1 it will work, but 4.3 it will barf with an NPE. In other words, when
reading the XML on 4.1 it seems to leave any fields alone that are not
represented in the XML, whereas on 4.3 it seems to assign null to them.

I've got around the problem by

if ( myMessages != null && myMessages.size()>0 )

but I'd have expected it to be consistent.


Regards,


Charles.




---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Jörg Schaible
2014-09-17 13:07:53 UTC
Permalink
Hi Charles,

sorry for the delay, working on the backlog ...
Post by Charles Colbourn
Hi
I've stumbled over a bit of an oddity with Xstream across these two
versions of Android.
actually I have no Android, so I can only assume ...
Post by Charles Colbourn
@XStreamImplicit
public ArrayList<String> myMessages = new ArrayList<String>();
and you read XML using Xstream.fromXML(string);
if ( myMessages.size()>0 ) {#dostuff
On 4.1 it will work, but 4.3 it will barf with an NPE. In other words,
when reading the XML on 4.1 it seems to leave any fields alone that are
not represented in the XML, whereas on 4.3 it seems to assign null to
them.
I've got around the problem by
if ( myMessages != null && myMessages.size()>0 )
but I'd have expected it to be consistent.
It looks like XStream does not run in enhanced mode on the older Android
version (requires stuff from sun.misc.Unsafe). In that case it cannot
instantiate objects without running the initializers. If your implicit list
contains no element, XStream does not know about the list member at all at
deserialization time and leaves it alone. So the list instance must have
been created by the Java initializers of the class.

Which version of XStream are you using? Do you have some kind of console
where you can run com.thoughtworks.xstream.core.JVM and compare the output?

Cheers,
Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Loading...