2014-04-21 33 views
3

我有一個運行在虛擬機上的Hazelcast服務器實例。數據應該存儲在MAP<Integer, User>,其中User類是如下:Hazelcast IMap.get()拋出HazelcastSerializationException,但IMap.put()工作正常

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) 

回答

9

從它看起來像你的類沒有默認構造函數(無參數的構造函數)的堆棧跟蹤。如果您已經使用參數集定義了構造函數,則默認構造函數不是由編譯器自動創建的,您必須明確定義它。 或者,您的類或構造函數可能不是公共作用域,但是包私有的或私有的。

public class User { 

    // Fields 

    public User() { 
    } 

    // Getters/Setters 
} 
+0

這就是我的解決方案,非常感謝! – antonio

相關問題