NullPointerException with HashMap in 2.23.1

306 views
Skip to first unread message

Robert DiFalco

unread,
May 5, 2014, 6:58:19 PM5/5/14
to kryo-...@googlegroups.com
I will investigate this further but anyone know off the top of their head why I might be getting this error when switching from 2.23.0 to 2.23.1? All serialized data is transient between versions.

Caused by: java.lang.NullPointerException: null
at java.util.HashMap.put(HashMap.java:472) ~[na:1.7.0_09]
at com.esotericsoftware.kryo.serializers.MapSerializer.read(MapSerializer.java:144) ~[kryo-2.23.1.jar:na]
at com.esotericsoftware.kryo.serializers.MapSerializer.read(MapSerializer.java:21) ~[kryo-2.23.1.jar:na]
at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:679) ~[kryo-2.23.1.jar:na]
at com.esotericsoftware.kryo.serializers.ObjectField.read(ObjectField.java:106) ~[kryo-2.23.1.jar:na]
... 90 common frames omitted

Robert DiFalco

unread,
May 5, 2014, 7:07:16 PM5/5/14
to kryo-...@googlegroups.com
A quick look shows that StdInstantiatorStrategy is being used to create a map and it is not initializing the class properly. It gets instantiated as new Object() so when kryo tries to access this.table (to do a put operation in the deserializer) I get an NPE. 

I set my Kryo instantiator strategy to use the StdInstantiatorStrategy. In the past this was not an issue. 

When I get some time tomorrow I will try to isolate this into a small unit test.

Robert DiFalco

unread,
May 5, 2014, 9:50:24 PM5/5/14
to kryo-...@googlegroups.com
Okay, here's a simple Test case:


    @Test
    public void testHashMapSerialization() {
        Kryo kryo = new Kryo();
        kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());

        Map map = new HashMap();
        map.put("foo", "bar");

        Output out = new Output(1024, 1024);
        kryo.writeClassAndObject(out, map);

        Input in = new Input( out.toBytes() );
        Map read = (Map)kryo.readClassAndObject(in);

        Assert.assertEquals(map, read);
    }


This will result in an NPE.


On Monday, May 5, 2014 3:58:19 PM UTC-7, Robert DiFalco wrote:

ivan.m...@gmail.com

unread,
Jun 5, 2014, 7:28:48 PM6/5/14
to kryo-...@googlegroups.com
Just to follow up. This is a known issue.

Workaround is to replace 

       kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());

with 
((Kryo.DefaultInstantiatorStrategy) kryo.getInstantiatorStrategy()).setFallbackInstantiatorStrategy(new StdInstantiatorStrategy());
On Monday, May 5, 2014 3:58:19 PM UTC-7, Robert DiFalco wrote:
Reply all
Reply to author
Forward
0 new messages