简体   繁体   English

反序列化ArrayList。 没有有效的构造函数

[英]Deserializing an ArrayList. no valid constructor

This how I deserialize my arrayList which contains objects of identification 这就是我反序列化包含标识对象的arrayList的方式

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

This is how I serialize it 这就是我序列化的方式

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)

This is line 92 这是第92行

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

Why is this happening? 为什么会这样呢?

This is the code of Identification Object. 这是Identification对象的代码。

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

The problem is --> in java 问题是->在Java中

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

And in your class you are calling super class constructor which is not implements Serializable. 并且在您的类中,您正在调用未实现Serializable的super类构造函数。

So that was the problem.. :) 所以这就是问题.. :)

For your Second question take a look JavaDoc 对于第二个问题,请看一下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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM