在将扩展加载到程序期间,我遇到了一个主要问题。因为没有有效的构造函数,我得到一个异常。
问题在于:
ekstensja = (ArrayList<Dydaktyk>) ois.readObject();
我得到类似的东西:
java.io.InvalidClassException: Dydaktyk; no valid constructor
at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(Unknown Source)
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 Dydaktyk.wczytajEkstensje(Dydaktyk.java:81)
at Dydaktyk.<clinit>(Dydaktyk.java:69)
at java.io.ObjectStreamClass.hasStaticInitializer(Native Method)
at java.io.ObjectStreamClass.computeDefaultSUID(Unknown Source)
at java.io.ObjectStreamClass.access$100(Unknown Source)
at java.io.ObjectStreamClass$1.run(Unknown Source)
at java.io.ObjectStreamClass$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.io.ObjectStreamClass.getSerialVersionUID(Unknown Source)
at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(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 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.defaultReadFields(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 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 Przedmiot.wczytajEkstensje(Przedmiot.java:99)
at Przedmiot.<clinit>(Przedmiot.java:87)
at GUI.main(GUI.java:100)
static {
wczytajEkstensje(); // load Extension
}
public static void wczytajEkstensje() {// load extension
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("dydaktyk.ser");
ois = new ObjectInputStream(fis);
// here is the problem
ekstensja = (ArrayList<Dydaktyk>) ois.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (ois != null) {
ois.close();
}
} catch (IOException e) {
}
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
}
}
}
public static void zapiszEkstensje() {// save extension
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("dydaktyk.ser");
oos = new ObjectOutputStream(fos);
oos.writeObject(ekstensja); // serialization
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (oos != null) {
oos.close();
}
} catch (IOException e) {
}
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
}
}
}
答案 0 :(得分:9)
类Dydaktyk
应该具有可访问(公共或受保护)的无参数构造函数,以便序列化反射机制可以创建类的实例:
public Dydaktyk() {
...
}
来自docs
在反序列化期间,将使用类的public或protected no-arg构造函数初始化非可序列化类的字段。必须可以对可序列化的子类访问no-arg构造函数。可序列化子类的字段将从流中恢复。
答案 1 :(得分:2)
如Oracle网站中关于Serialization
interface:
在反序列化期间,非序列化类的字段将是 使用公共或受保护的无参数构造函数初始化 类。 子类必须可以访问no-arg构造函数 序列化即可。将恢复可序列化子类的字段 来自溪流。
我认为Dydaktyk
是某类的子类。您已在parametric constructor
中定义了superclass
,但未在其中定义parameter-less constructor
。并且编译器没有为superclass
插入默认构造函数。因此,您还应该在超类中定义parameter-less constructor
。