我在VM上运行了Hazelcast服务器实例。数据应该存储在MAP<Integer, User>
中,其中User类如下:
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();
}
我使用客户端实例连接到此服务器并尝试将对象添加到该映射:
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());
上面的代码为我提供了以下控制台输出:
Map Size: 0
Map Size: 1
yes
yes
以下行User queried = (User) map.get(1);
的例外:
Problem while reading DataSerializable, namespace: 0, id: 0, class: com.blabla.User, exception: com.blabla.User.<init>()
这是什么问题?为什么我无法读取我刚才放在地图中的数据?
以下是例外情况:
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)
答案 0 :(得分:9)
从堆栈跟踪看来,您的类似乎没有默认构造函数(没有参数的构造函数)。如果已使用参数集定义了构造函数,则编译器不会自动创建默认构造函数,您必须明确定义它。 或者,您的类或构造函数可能不是公共作用域,而是包私有或私有。
public class User {
// Fields
public User() {
}
// Getters / Setters
}