有没有办法确保对象的序列化正确完成?
我创建了一个我用writeObject()
方法序列化的applet,我没有异常没有错误。
但是当我尝试用readObject()
反序列化它时,我得到了一个NullPointerException。
我的applet有点复杂,但我确保我的所有字段都是可序列化的。
异议的类有嵌套类。
对可能发生的事情有任何猜测?
下面的代码在我的servlet中创建了一个SerialTree。
` SerialTree st = new SerialTree() ;
try{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("/home/cof/test8"));
out.writeUnshared(st);
out.flush();
out.close();
}
这是堆栈跟踪:
java.lang.NullPointerException
at java.awt.Container.readObject(Container.java:3575)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:991)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1865)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1770)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1684)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1340)
at java.io.ObjectInputStream.access$300(ObjectInputStream.java:205)
at java.io.ObjectInputStream$GetFieldImpl.readFields(ObjectInputStream.java:2125)
at java.io.ObjectInputStream.readFields(ObjectInputStream.java:536)
at java.awt.Container.readObject(Container.java:3560)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:991)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1865)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1770)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1684)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1340)
at java.io.ObjectInputStream.access$300(ObjectInputStream.java:205)
at java.io.ObjectInputStream$GetFieldImpl.readFields(ObjectInputStream.java:2125)
at java.io.ObjectInputStream.readFields(ObjectInputStream.java:536)
at java.awt.Container.readObject(Container.java:3560)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:991)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1865)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1770)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1684)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1340)
at java.io.ObjectInputStream.access$300(ObjectInputStream.java:205)
at java.io.ObjectInputStream$GetFieldImpl.readFields(ObjectInputStream.java:2125)
at java.io.ObjectInputStream.readFields(ObjectInputStream.java:536)
at java.awt.Container.readObject(Container.java:3560)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:991)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1865)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1770)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1684)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1340)
at java.io.ObjectInputStream.access$300(ObjectInputStream.java:205)
at java.io.ObjectInputStream$GetFieldImpl.readFields(ObjectInputStream.java:2125)
at java.io.ObjectInputStream.readFields(ObjectInputStream.java:536)
at java.awt.Container.readObject(Container.java:3560)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:991)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1865)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1770)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1684)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1340)
at java.io.ObjectInputStream.access$300(ObjectInputStream.java:205)
at java.io.ObjectInputStream$GetFieldImpl.readFields(ObjectInputStream.java:2125)
at java.io.ObjectInputStream.readFields(ObjectInputStream.java:536)
at java.awt.Container.readObject(Container.java:3560)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:991)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1865)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1770)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1684)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1340)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1963)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1887)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1770)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:368)
at arbre.ArbreClient.getHttpObject(ArbreClient.java:92)
at arbre.ArbreClient.init(ArbreClient.java:123)
at sun.applet.AppletPanel.run(AppletPanel.java:436)
at java.lang.Thread.run(Thread.java:636)
这里是applet本身的第70到106行,它试图检索一个serilaTree对象?
` 尝试{ result = new ObjectInputStream(new FileInputStream(“/ home / cof / test8”));
}
catch(NotSerializableException e){
e.printStackTrace();
}
catch(InvalidClassException e){
e.printStackTrace();
}
catch(StreamCorruptedException e){
e.printStackTrace();
}
catch(NotActiveException e){
e.printStackTrace();
}
catch(OptionalDataException e){
e.printStackTrace();
}
Object obj =null;
try{
obj = result.readObject();
}
catch(NullPointerException e){
e.printStackTrace();
// System.out.println("lalal");
}
catch(NotSerializableException e){
e.printStackTrace();
}
catch(InvalidClassException e){
e.printStackTrace();
}
catch(StreamCorruptedException e){
e.printStackTrace();
}
答案 0 :(得分:6)
对我来说,对Applet类型进行ser / deser是没有意义的。尝试使用其他类。
<强>更新强>
我已经阅读了你的stacktrace,我看到的是你在运行applet时试图反序列化applet。我强烈怀疑这是你问题的原因。尝试将所有变量移动到另一个类中(例如,将其称为MySerializableApplet
,并且不要使其扩展类Applet
),然后仅在此类上运行序列化/反序列化。