Sat
Sat

Reputation: 89

My class is Serializable and i have java.io.NotSerializableException why?

I have this exception and I don't understand why. The error log :

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)

The main class, the problem seems to be here at out.writeObject(out); so i suppose it's a output format problem or something like that :

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();
    }
}    
}

My class Equipe :

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;
 }

If anyone can help, i thought the error was just the Equipe implements but i seems like it's not.

Upvotes: -1

Views: 229

Answers (1)

Turing85
Turing85

Reputation: 20195

You try to write out (aka the ObjectStream) into itself. You probably meant to write str into out:

out.writeObject(str);

Upvotes: 2

Related Questions