2014-01-09 46 views
2

我在嘗試發送以下內容,但在下面出現以下錯誤消息。 TableUser實現可序列化,但似乎問題是FXCollections,但我不知道如何序列化。java.io.NotSerializableException異常問題

這裏是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) 
+0

,xmlEncoder適用於可觀察列表。我將它們編碼爲zip文件,以獲得大小和速度。你可能不得不改變私人靜態,我不記得了。 – brian

回答

1

鑑於ObservableListWrapper不可序列化,你可以嘗試通過您的TableUser對象循環,並增加他們逐一data

事情是這樣的:萬一你還沒有嘗試過

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

完美!謝謝! –

1

一個NotSerializableException當試圖進行序列化對象沒有實現Serializable類拋出。在你的情況,因爲它告訴你,ObservableListWrapper是不可序列化的,我會推斷clientList是不可序列化的。

除了使用JavaFX ObservableList之外,您還可以使用一個列表,如java.util.ArrayList,它恰好實現了Serializable

+0

爲了不得不更新表格,它必須是可觀察列表。 :\ –

+1

您可以使用'FXCollections.observableList(List)'將列表轉換爲可觀察列表。 – mattbdean

+0

@thatJavaNerd我使用'FXCollections.observableList(List)',但它不會更新爲「true」ObservableList會更新。它看起來像等待另一個事件發生,然後更新。 – Bartic