【问题标题】:An error: java.io.NotSerializableException一个错误:java.io.NotSerializableException
【发布时间】:2014-03-10 12:01:13
【问题描述】:

我有一个类定义如下

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;


}

这样做

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

给我java.io.NotSerializableException 有什么问题?

【问题讨论】:

  • 如果要将对象转储为字节序列,则该类必须实现 Serializable 标记接口。
  • 需要异常的堆栈跟踪。否则无法确定问题所在。
  • 现在没问题,我需要out.writeObject(ae);,谢谢

标签: java serialization


【解决方案1】:

你必须在类定义中实现Serializable

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;

}

【讨论】:

    【解决方案2】:

    你需要实现Serializable标记接口

     public class EvaluationResult implements Serializable { .... }
    

    您应该编写对象(而不是文件流):

     out.writeObject(ae);
    

    【讨论】:

      【解决方案3】:

      您必须实现 Serializable 接口...这意味着实现以下内容:

      在序列化和反序列化过程中需要特殊处理的类必须实现具有这些确切签名的特殊方法:

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

      这个tutorial may be of use

      【讨论】:

        猜你喜欢
        • 2018-08-17
        • 2012-01-04
        • 2020-03-13
        • 1970-01-01
        • 2013-04-10
        • 1970-01-01
        • 1970-01-01
        • 2015-03-21
        • 1970-01-01
        相关资源
        最近更新 更多