我的类是可序列化的,我有java.io.NotSerializableException为什么?
我有这个例外,我不明白为什么。错误日志:我的类是可序列化的,我有java.io.NotSerializableException为什么?,java,javafx,serialization,objectoutputstream,notserializableexception,Java,Javafx,Serialization,Objectoutputstream,Notserializableexception,我有这个例外,我不明白为什么。错误日志: java.io.NotSerializableException: java.io.ObjectOutputStream at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348) at handballa
java.io.NotSerializableException: java.io.ObjectOutputStream
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at handballapp.HandballApp.main(HandballApp.java:62)
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:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
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:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
在主类中,问题似乎出现在out.writeObject(out)中所以我认为这是一个输出格式问题或类似的问题:
public class HandballApp extends Application{
public static void main(String[] args) {
ArrayList<Equipe> str = new ArrayList<>();
str.add(new Equipe("Paris-SG", "D1", 1, 22, 11, 11, 0, 0, 378, 301, "Raul Gonzalez Gutierrez", "Jesus Gonzalez Fernandez"));
File f = new File("team.txt");
try {
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(out);
out.close();
fos.close();
System.out.println("Data write successfully !");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
如果有人能帮忙的话,我认为错误只是Equipe实现的,但我似乎不是。您尝试将
输出
(也称对象流
)写入自身。您可能打算将str
写入out
:
out.writeObject(str);
您尝试将
输出(也称为文件)写入自身。你可能想写str
。图灵85是正确的。您实际上是在告诉一个ObjectOutputStream
对象进行自身序列化。谢谢!我只是用错了变量,我想我需要一些睡眠^^非常感谢。我只是用错了变量,我想我需要一些睡眠^^out
是ObjectOutputStream
,而不是文件。更正。
out.writeObject(str);