首页 > 解决方案 > 我的课程是可序列化的,我有 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 工具,但我似乎不是。

标签: javajavafxserializationobjectoutputstreamnotserializableexception

解决方案


您尝试将out(又名ObjectStream)写入自身。您可能打算str写入out

out.writeObject(str);

推荐阅读