gpt4 book ai didi

java.io.NotSerializableException 问题

转载 作者:行者123 更新时间:2023-11-30 09:13:30 27 4
gpt4 key购买 nike

我正在尝试发送以下内容,但出现以下错误。 TableUser 实现了可序列化,但问题似乎出在 FXCollections 上,但我不知道如何序列化它。

这是 TableUser 类。

package application;

import java.io.Serializable;


public class TableUser implements Serializable{

private static final long serialVersionUID = 1L;
private String username = "";

public TableUser(String name) {
this.username = name;
}

public String getUsername(){
return username;
}

public void setUsername(String user){
username = user;
}

}






//NOT apart of TableUser - This is the code that isn't working
private static ObservableList<TableUser> clientList = FXCollections.observableArrayList();

Object[] data = new Object[2];
data[0] = "CLIENTS";
data[1] = clientList;

for(int i = 0; i < clients.size(); i++){
clients.get(i).sendData(data);
}


//I don't know if this helps but here is the sendData method
protected void sendData(Object[] data){
try {
oos.writeObject(data); //ServerMultiClient.java:286
oos.reset();
} catch (IOException e) {
e.printStackTrace();
}
}


//This is from the Client application that is also part of the issue
if((fromServer = (Object[]) ois.readObject()) != null){ //Controller.java:109


java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: com.sun.javafx.collections.ObservableListWrapper
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readArray(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at application.ChatRoomController$2.run(Controller.java:109)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.NotSerializableException: com.sun.javafx.collections.ObservableListWrapper
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeArray(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at application.ServerMultiClient.sendData(ServerMultiClient.java:286)
at application.ServerMultiClient.run(ServerMultiClient.java:236)

最佳答案

鉴于 ObservableListWrapper 不可序列化,您可以尝试遍历 TableUser 对象,并将它们一一添加到 data

是这样的:

Object[] data = new Object[clientList.size()+1];
data[0] = "CLIENTS";
int counter = 1;
for(TableUser tu: clientList) {
data[counter] = tu;
counter++;
}

关于java.io.NotSerializableException 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21009813/

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