gpt4 book ai didi

java - 尽管我已经实现了 'Serializable',但为什么我收到 NotSerializedException 异常

转载 作者:行者123 更新时间:2023-12-02 06:30:27 24 4
gpt4 key购买 nike

我已经在“userInfo”类中实现了 Serialized。但我还是有异常(exception)。这是输出控制台 -

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)

这是我的两个类(class):

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;
}
}

最佳答案

why I'm getting NotSerializableException exception though I've implemented 'Serializable'

你还没有。

java.io.NotSerializableException: chatservernazmus.User public class userInfo implements Serializable{ public class User{

User 类也必须是可序列化的。

关于java - 尽管我已经实现了 'Serializable',但为什么我收到 NotSerializedException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20108254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com