I get a NullPointerException when I unserialize an ArrayList object using kryo.
Caused by: java.lang.NullPointerException
at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:215)
at java.util.ArrayList.ensureCapacity(ArrayList.java:199)
at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:96)
at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:22) at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:679)
at com.esotericsoftware.kryo.serializers.ObjectField.read(ObjectField.java:106)
I can see that StdInstantiatorStrategy creates an ArrayList without calling its constructor leaving one of the fields uninitialized causing the exception.
The documentation says that the no argument constructor should be called first and if none is available, the StdInstantiatorStrategy should be used to do field by field initialization.
What am I doing wrong?
With kryo version 2.24, calling
kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
overrides the default instantiator strategy which uses the class no argument constructor if present. The correct way to do this is to call:
((Kryo.DefaultInstantiatorStrategy) kryo.getInstantiatorStrategy()).setFallbackInstantiatorStrategy(new StdInstantiatorStrategy());
This is explained here https://github.com/EsotericSoftware/kryo
I think this has changed since version 2.21