java - 我的课程是可序列化的,我有 java.io.NotSerializableException 为什么?
问题描述
我有这个例外,我不明白为什么。错误日志:
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
(又名ObjectStream
)写入自身。您可能打算str
写入out
:
out.writeObject(str);
推荐阅读
- r - 如何转换这个小标题?
- c++ - 测试用例覆盖率和控制台应用程序覆盖率有什么区别?
- javascript - splitTextToSize 与 jspdf html?
- reactjs - 在添加之前根据长度管理 setState
- nginx - Nginx+Lua+s3文件上传
- amazon-web-services - Cognito 授权方给出 {"message": "unauthorized"}
- google-apps-script - 谷歌表格脚本检查登陆页面是否有“缺货”之类的短语
- redis - 组件内部 API 调用的 Nuxtjs Redis 缓存实现
- netsuite - 如何使项目始终显示在列表的最后(Netsuite 高级 PDF)
- c++ - 比较 char 和 char[i] 在hangman 游戏中不起作用