小编典典

为什么我实现了'Serializable'却得到NotSerializableException异常

java

我已经在“ userInfo”类中实现了Serializable。我仍然感到异常。这是输出控制台-

java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: chatservernazmus.User
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1333)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
        at java.util.ArrayList.readObject(ArrayList.java:593)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1849)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
        at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1947)
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1871)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
        at chatservernazmus.Server.getAllUser(Server.java:74)
        at chatservernazmus.Server.<init>(Server.java:44)
        at chatservernazmus.Main.main(Main.java:11)

这是我的两节课:

public class userInfo implements Serializable{
    public ArrayList<User> allUser=new ArrayList<User>();

    public void addUser(User e){
        allUser.add(e);
    }
    public ArrayList<User> getAllUser(){
     return allUser;
    }
}


public class User{
    public String userName="";
    public String password="";
    public String statusMessage="";
    public ArrayList<String> friend=new ArrayList<String>();
    public ArrayList<String>offlineMessage=new ArrayList<String>();

    public User(String username,String password,String message){
        userName=username;
        password=password;
        statusMessage=message;
    }

    public void setStatusMessage(String message){
        statusMessage=message;
    }
    public void addFriend(String fr){
        friend.add(fr);
    }
    public ArrayList<String> getOfflineMessage(){
        ArrayList<String> str=new ArrayList<String>();
        offlineMessage.clear();
        return str;
    }
}

阅读 725

收藏
2020-11-30

共1个答案

小编典典

为什么我实现了’Serializable’却得到NotSerializableException异常

你还没有

java.io.NotSerializableException:chatservernazmus.User公共类userInfo实现了Serializable
{公共类User {

User类必须是可序列化也是如此。

2020-11-30