【发布时间】:2023-04-07 17:52:01
【问题描述】:
我有这个例外,我不明白为什么。错误日志:
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();
}
}
}
我的班级装备:
public class Equipe implements Serializable{
private String nom;
private String ligue;
private int classement;
private int pts;
private int matchJouer;
private int victoire;
private int defaite;
private int matchNul;
private int butPour;
private int butContre;
private String entraineur;
private String entraineurAdj;
}
如果有人可以提供帮助,我认为错误只是 Equipe 工具,但我似乎不是。
【问题讨论】:
-
您尝试将
out(又名文件)写入自身。你可能打算写str。 -
Turing85 是正确的。你实际上告诉
ObjectOutputStream对象序列化自己。 -
谢谢!我只是使用了错误的变量,我想我需要一些睡眠...... ^^
标签: java javafx serialization objectoutputstream notserializableexception