java.io.InvalidClassException:没有有效的构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9747443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:05:04  来源:igfitidea点击:

java.io.InvalidClassException: no valid constructor

java

提问by nitdgp

When I run below program, I am getting exception as

当我在程序下面运行时,我收到异常

java.io.InvalidClassException: Files.SerializationMain; Files.SerializationMain; 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 Files.SerializationClass.main(SerializationClass.java:71)
Caused by: java.io.InvalidClassException: Files.SerializationMain; no valid constructor
    at java.io.ObjectStreamClass.<init>(Unknown Source)
    at java.io.ObjectStreamClass.lookup(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at Files.SerializationClass.main(SerializationClass.java:61)

I read somewhere that when we serialize any child class then its base class constructor gets fired.

我在某处读到,当我们序列化任何子类时,它的基类构造函数就会被触发。

class Parent123
{
    int age;
    String name;

    Parent123(int age,String name) {
        System.out.println("We are in Parent123 Constructor");
        this.age=age;
        this.name=name;
    }  
}

class SerializationMain extends Parent123 implements Serializable {
    int data1;
    String data2;

    SerializationMain(int data1,String data2)
    {
        super(20,"test");
        this.data1=data1;
        this.data2=data2;
    }

    public void setData1(int data1)
    {
        this.data1=data1;
    }
    public void setData2(String data2)
    {
        this.data2=data2;
    }
    public String getData2()
    {
        return data2;
    }
    public int getData1()
    {
        return data1;
    }
}

public class SerializationClass {

    public static void main(String args[])
    {
        System.out.println("Before Creating Object");
        SerializationMain s1=new SerializationMain(10,"Anurag");
        try
        {
            System.out.println("Serializing Object");
            FileOutputStream fis=new FileOutputStream("Test.ser");
            ObjectOutputStream ois=new ObjectOutputStream(fis);
            ois.writeObject(s1);
        } catch(Exception e1) {
            e1.printStackTrace();
        }
        try
        {
            FileInputStream fis=new FileInputStream("Test.ser");
            ObjectInputStream ois=new ObjectInputStream(fis);
            Object o1=ois.readObject();
            SerializationMain s2=(SerializationMain)o1;
        }
        catch(Exception e1)
        {
            e1.printStackTrace();
        }
    }
}//End of SerializationClass

回答by Jigar Joshi

Just provide default constructor in both classes (Parent & Child)

只需在两个类(父和子)中提供默认构造函数

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. more

在反序列化期间,不可序列化类的字段将使用类的公共或受保护的无参数构造函数进行初始化。可序列化的子类必须可以访问无参数构造函数。可序列化子类的字段将从流中恢复。更多的

回答by hey

Add implementation of Serializable to parent class.

将 Serializable 的实现添加到父类。

回答by amarnath harish

No, constructors are not at all called if you use Serialization process. Serializable uses reflection to construct object and does not require no arg constructor. But Externalizable requires public no-arg constructor.however, you parent class may require no-arg constructor.refer this article

不,如果您使用序列化过程,则根本不会调用构造函数。 Serializable 使用反射来构造对象,不需要 arg 构造函数。但是 Externalizable 需要公共无参数构造函数。但是,您的父类可能需要无参数构造函数。请参阅本文

An object is serializable (itself implements the Serializable interface) even if its superclass is not. However, the first superclass in the hierarchy of the serializable class, that does not implements Serializable interface, MUST have a no-arg constructor. If this is violated, readObject() will produce a java.io.InvalidClassException in runtime.

一个对象是可序列化的(它本身实现了 Serializable 接口),即使它的超类不是。但是,可序列化类层次结构中的第一个超类,没有实现 Serializable 接口,必须有一个无参数构造函数。如果违反, readObject() 将在运行时产生 java.io.InvalidClassException 。

you wouldn't get this error if you make the parent class serializable or if you provide public no-arg constructor in parent class.

如果您使父类可序列化或者在父类中提供公共无参数构造函数,则不会出现此错误。