java – kryo. ReadObject causes NullPointerException with ArrayList

When I use kryo to deserialize the ArrayList object, I get a NullPointerException

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, and one of the fields is uninitialized, resulting in an exception

The document says that the parameterless constructor should be called first. If it is not available, stdinstantiatorstrategy should be used for field initialization

What on earth did I do wrong?

Solution

With kryo version 2.24, call

kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());

Override the default instantiator policy (if any) using the class no argument constructor The correct way 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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>