gpt4 book ai didi

java.io.InvalidClassException : no valid constructor

转载 作者:IT老高 更新时间:2023-10-28 20:54:49 27 4
gpt4 key购买 nike

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

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)

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

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

最佳答案

将 Serializable 的实现添加到父类。

关于java.io.InvalidClassException : no valid constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9747443/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com