william007
william007

Reputation: 18545

An error: java.io.NotSerializableException

I have a class define as follow

public class EvaluationResult implements java.io.Serializable {
    public String ClassName;
    public boolean FeedbackDirected;
    public double HV;
    public double Spread;
    public double PercentageOfCorrectness;
    public double TimeElapsed;
    public ArrayList<double[]> ParetoFront;


}

Doing this

public static void main(String[] args) throws Exception {
         EvaluationResult ae=new EvaluationResult();
         FileOutputStream fileOut =new FileOutputStream("result.txt");
         ObjectOutputStream out = new ObjectOutputStream(fileOut);
         out.writeObject(fileOut);
         out.close();
         fileOut.close();
     }

gives me java.io.NotSerializableException what is the problem?

Upvotes: 1

Views: 3927

Answers (3)

Eugene
Eugene

Reputation: 120858

you need to implement Serializable marker interface

 public class EvaluationResult implements Serializable { .... }

You should be writing the object (and not the file stream) :

 out.writeObject(ae);

Upvotes: 3

ErstwhileIII
ErstwhileIII

Reputation: 4843

You must implement the Serializable interface ... this implies implementing the following:

Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:

 private void writeObject(java.io.ObjectOutputStream out) throws IOException
 private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;
 private void readObjectNoData() throws ObjectStreamException;

This tutorial may be of use

Upvotes: 1

blackpanther
blackpanther

Reputation: 11486

You must implement Serializable in the class definition:

public class EvaluationResult implements Serializable {
    public String ClassName;
    public boolean FeedbackDirected;
    public double HV;
    public double Spread;
    public double PercentageOfCorrectness;
    public double TimeElapsed;
    public ArrayList<double[]> ParetoFront;

}

Upvotes: 3

Related Questions