dictionaryhazelcastdata-serialization

Hazelcast IMap.get() throws HazelcastSerializationException, but IMap.put() works fine


I have an Hazelcast server instance running on a VM. Data is supposed to be stored in a MAP<Integer, User>, where the User class is as following:

public class User implements com.hazelcast.nio.serialization.DataSerializable{
    private Integer id;
    private String name;
    private String nick;
    private Boolean sex;

    //getters & setters

    @Override
    public void writeData(ObjectDataOutput out) throws IOException{
        out.writeInt(id.intValue());
        out.writeUTF(name);
        out.writeUTF(nick);
        out.writeBoolean(sex);
    }

    @Override
    public void readData(ObjectDataInput in) throws IOException{       
        id = (Integer) in.readInt();
        name = in.readUTF();
        nick = in.readUTF();
        sex = in.readBoolean();
    }

I connect to this server with a client instance and try to add an object to that map:

    System.out.println("Map Size: " + map.size());
    map.put(1, user);
    System.out.println("Map Size: " + map.size());

    System.out.println(map.containsKey(1) ? "yes":"no");
    System.out.println(map.containsValue(user) ? "yes":"no");
    User queried = (User) map.get(1);    /*this is line 64*/

    System.out.println(queried.toString());

The upper code gives me the following console output:

    Map Size: 0
    Map Size: 1
    yes
    yes

And the following Exception for the line User queried = (User) map.get(1);:

Problem while reading DataSerializable, namespace: 0, id: 0, class: com.blabla.User, exception: com.blabla.User.<init>()

What is here the problem? Why can't I read the data I just put in the Map?

Here are the exception details:

com.hazelcast.nio.serialization.DataSerializer.read(DataSerializer.java:114)
    com.hazelcast.nio.serialization.DataSerializer.read(DataSerializer.java:36)
    com.hazelcast.nio.serialization.StreamSerializerAdapter.read(StreamSerializerAdapter.java:59)
    com.hazelcast.nio.serialization.SerializationServiceImpl.toObject(SerializationServiceImpl.java:218)
    com.hazelcast.client.spi.impl.ClientClusterServiceImpl._sendAndReceive(ClientClusterServiceImpl.java:172)
    com.hazelcast.client.spi.impl.ClientClusterServiceImpl.sendAndReceive(ClientClusterServiceImpl.java:137)
    com.hazelcast.client.spi.impl.ClientInvocationServiceImpl.invokeOnTarget(ClientInvocationServiceImpl.java:42)
    com.hazelcast.client.spi.impl.ClientInvocationServiceImpl.invokeOnKeyOwner(ClientInvocationServiceImpl.java:53)
    com.hazelcast.client.proxy.ClientMapProxy.invoke(ClientMapProxy.java:492)
    com.hazelcast.client.proxy.ClientMapProxy.get(ClientMapProxy.java:83)
    com.blabla.HazelcastFactory.insertUser(HazelcastFactory.java:64)

java.lang.NoSuchMethodException: com.dileky.User.<init>()
    java.lang.Class.getConstructor0(Class.java:2800)
    java.lang.Class.getDeclaredConstructor(Class.java:2043)
    com.hazelcast.nio.ClassLoaderUtil.newInstance(ClassLoaderUtil.java:54)
    com.hazelcast.nio.ClassLoaderUtil.newInstance(ClassLoaderUtil.java:50)
    com.hazelcast.nio.serialization.DataSerializer.read(DataSerializer.java:103)
    com.hazelcast.nio.serialization.DataSerializer.read(DataSerializer.java:36)
    com.hazelcast.nio.serialization.StreamSerializerAdapter.read(StreamSerializerAdapter.java:59)
    com.hazelcast.nio.serialization.SerializationServiceImpl.toObject(SerializationServiceImpl.java:218)
    com.hazelcast.client.spi.impl.ClientClusterServiceImpl._sendAndReceive(ClientClusterServiceImpl.java:172)
    com.hazelcast.client.spi.impl.ClientClusterServiceImpl.sendAndReceive(ClientClusterServiceImpl.java:137)
    com.hazelcast.client.spi.impl.ClientInvocationServiceImpl.invokeOnTarget(ClientInvocationServiceImpl.java:42)
    com.hazelcast.client.spi.impl.ClientInvocationServiceImpl.invokeOnKeyOwner(ClientInvocationServiceImpl.java:53)
    com.hazelcast.client.proxy.ClientMapProxy.invoke(ClientMapProxy.java:492)
    com.hazelcast.client.proxy.ClientMapProxy.get(ClientMapProxy.java:83)
    com.blabla.HazelcastFactory.insertUser(HazelcastFactory.java:64)

Solution

  • From the stacktrace it seems like your class does not have a default constructor (a constructor without a parameter). If you have defined a constructor with a parameter set the default constructor is not created by the compiler automatically and you would have to define it explicitly. Alternatively your class or constructor might not be public scoped but package-private or private.

    public class User {
    
      // Fields
    
      public User() {
      }
    
      // Getters / Setters
    }