我的班级是可序列化的,为什么我有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,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实施的,但我似乎没有。

bjkao2006 回答:我的班级是可序列化的,为什么我有java.io.NotSerializableException?

您尝试将out(也称为ObjectStream)写入自身。您可能打算将str写入out

out.writeObject(str);
本文链接:https://www.f2er.com/2955540.html

大家都在问