【问题标题】:InvalidClassException when opening a serialized object in java在 java 中打开序列化对象时出现 InvalidClassException
【发布时间】:2012-12-25 15:45:28
【问题描述】:

好的,所以我有一个要保存的 Region 类对象。我已经使 Region 和 Region 类中的所有类都实现了可序列化。我的保存方法工作得很好,但是当我尝试使用以下方法打开保存的文件时:

// ----------------------------------------------------------
/**
 * Open a Region object with the given fileName.
 *
 * @param fileName
 * @return The desired Region object.
 */
public static Region openRegion(String fileName)
{
    try
    {
        FileInputStream fis = new FileInputStream(fileName + ".txt");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object readObject = ois.readObject();
        ois.close();

        if (readObject != null && readObject instanceof Region)
        {
            return (Region)readObject;
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    catch (ClassNotFoundException e)
    {
        e.printStackTrace();
    }
    return null;
}

然后我收到以下错误:java.io.InvalidClassException: htm.model.Cell;没有有效的构造函数 在 java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(未知来源) 在 java.io.ObjectStreamClass.checkDeserialize(Unknown Source)

Cell 对象是 Region 对象中的一个类,但它肯定有一个构造函数,其工作方式如下: 公共单元格(列列,int columnIndex) { 超级(列,列索引); this.predictionSteps = 0; this.predictingState = false; this.previousPredictingState = false; this.learningState = 假; this.previousLearningState = false; this.listOfDistalSegments = new ArrayList(5); }

很抱歉这个问题很长,但我不知道我做错了什么。谢谢!

【问题讨论】:

    标签: serialization


    【解决方案1】:

    Cell 的最近的不可序列化基类必须有一个公共的无参数构造函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-01
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 2013-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多