我想在Java中发送一个带有ObjectOutputStream的对象。我有一个名为TicketList的类。
这是我的班级:
public class TicketList implements Serializable{
private ArrayList<Ticket> ticketlist;
//Konstruktor: Erstellt eine Liste mit einerm Benutzername
public TicketList(ArrayList<Ticket> list){
this.ticketlist = list;
}
//Standart Konstruktor
public TicketList(){
ticketlist = new ArrayList<>();
}
//Fügt der Liste ein neues Element hinzu
public void addToTicketList(Ticket ticket){
ticketlist.add(ticket);
}
//Gibt die Liste zurück
public List<Ticket> getTicketList(){
//Daten werden aus der Datenbank geladen
return ticketlist;
}
public Ticket getTicketByIndex(int index){
return ticketlist.get(index);
}
}
我收到此错误:
java.io.NotSerializableException: taraticketmodel.Ticket
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at java.util.ArrayList.writeObject(ArrayList.java:762)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at taraticketmodel.ClientManager.sendObjectToServer(ClientManager.java:28)
at taraticketclient.Service.send(Service.java:23)
at taraticketfrontendtest.MainTest.main(MainTest.java:45)
如果我用其他类测试它,它可以:(
应该实现可序列化的类是存储在 ArrayList 中的类,而不是用于构建 ArrayList 的类。