反序列化ArrayList。没有有效的构造函数 [英] Deserializing an ArrayList. no valid constructor

查看:115
本文介绍了反序列化ArrayList。没有有效的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是如何反序列化包含标识对象的arrayList

This how I deserialize my arrayList which contains objects of identification

public void deserializeArrayList(){
    String path = "./qbank/IdentificationHARD.quiz";
    try{
          FileInputStream fileIn = new FileInputStream(path);
            ObjectInputStream in = new ObjectInputStream(fileIn);
            ArrayList<Identification> list = (ArrayList<Identification>) in.readObject();
            System.out.println(list);
    }catch(Exception e){
        e.printStackTrace();
    }
}

这是我序列化的方式

public void saveItemIdentification(ArrayList<Identification> identification,File file){
    try{
        ObjectOutputStream out = new ObjectOutputStream(
                                      new FileOutputStream(file));
        out.writeObject(identification);
    }catch(Exception e){
        e.printStackTrace();
    }
}

但是当我反序列化它时它会给我这个错误

But when I deserialize it it gives me this errors

java.io.InvalidClassException: quizmaker.management.Identification; quizmaker.management.Identification; no valid constructor
    at java.io.ObjectStreamClass.checkDeserialize(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at java.util.ArrayList.readObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
    at java.io.ObjectInputStream.readSerialData(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at quizmaker.management.Manage.deserializeArrayList(Manage.java:92)

这是第92行

ArrayList<Identification> list = (ArrayList<Identification>) in.readObject();

为什么会发生这种情况?

Why is this happening?

这个是标识对象的代码。

 package quizmaker.management; 
 import java.io.Serializable;
 import quizmaker.Accounts.Rights.IAnswerable;

public class Identification extends Question implements Serializable{

    private static final long serialVersionUID = 2L;
    private String question;
    private String answer;

    public Identification(String q , String a){
        super(q,a);
    }

    public String toString(){
        return String.format("Question: %s\n Answer %s", getQuestion(),getAnswer());
    }
}


推荐答案

问题是 - >在java中

The problem is --> in java

Java serialization process  only continues in object hierarchy till the class
is Serializable i.e. implements Serializable interface in Java.

在你班上你打电话给 super 类构造函数,它不是实现Serializable。

And in your class you are calling super class constructor which is not implements Serializable.

所以这就是问题.. :)

So that was the problem.. :)

为你的第二个问题看看 JavaDoc

For your Second question take a look JavaDoc

During deserialization, the fields of non-serializable classes will be 
initialized using the public or protected no-arg constructor of the class.
A no-arg constructor must be accessible to the subclass that is serializable.
The fields of serializable subclasses will be restored from the stream.

这篇关于反序列化ArrayList。没有有效的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆