确保对象的序列化正确完成
有没有办法确保对象的序列化正确完成? 我创建了一个使用 writeObject()
方法序列化的小程序,没有异常,没有错误。 但是当我尝试使用 readObject() 对其进行反序列化时,我得到了 NullPointerException。 我的小程序有点复杂,但我确保所有字段都是可序列化的。
对象的类有嵌套类。
你猜会发生什么吗?
下面的代码在我的 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 在 java.awt.Container.readObject(Container.java:3575) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:616) 在 java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:991) 在 java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1865) 在 java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1770) 在 java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346) 在 java.io.ObjectInputStream.readArray(ObjectInputStream.java:1684) 在 java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1340) 在 java.io.ObjectInputStream.access$300(ObjectInputStream.java:205) 在 java.io.ObjectInputStream$GetFieldImpl.readFields(ObjectInputStream.java:2125) 在 java.io.ObjectInputStream.readFields(ObjectInputStream.java:536) 在 java.awt.Container.readObject(Container.java:3560) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:616) 在 java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:991) 在 java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1865) 在 java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1770) 在 java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346) 在 java.io.ObjectInputStream.readArray(ObjectInputStream.java:1684) 在 java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1340) 在 java.io.ObjectInputStream.access$300(ObjectInputStream.java:205) 在 java.io.ObjectInputStream$GetFieldImpl.readFields(ObjectInputStream.java:2125) 在 java.io.ObjectInputStream.readFields(ObjectInputStream.java:536) 在 java.awt.Container.readObject(Container.java:3560) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:616) 在 java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:991) 在 java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1865) 在 java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1770) 在 java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346) 在 java.io.ObjectInputStream.readArray(ObjectInputStream.java:1684) 在 java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1340) 在 java.io.ObjectInputStream.access$300(ObjectInputStream.java:205) 在 java.io.ObjectInputStream$GetFieldImpl.readFields(ObjectInputStream.java:2125) 在 java.io.ObjectInputStream.readFields(ObjectInputStream.java:536) 在 java.awt.Container.readObject(Container.java:3560) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:616) 在 java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:991) 在 java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1865) 在 java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1770) 在 java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346) 在 java.io.ObjectInputStream.readArray(ObjectInputStream.java:1684) 在 java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1340) 在 java.io.ObjectInputStream.access$300(ObjectInputStream.java:205) 在 java.io.ObjectInputStream$GetFieldImpl.readFields(ObjectInputStream.java:2125) 在 java.io.ObjectInputStream.readFields(ObjectInputStream.java:536) 在 java.awt.Container.readObject(Container.java:3560) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:616) 在 java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:991) 在 java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1865) 在 java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1770) 在 java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346) 在 java.io.ObjectInputStream.readArray(ObjectInputStream.java:1684) 在 java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1340) 在 java.io.ObjectInputStream.access$300(ObjectInputStream.java:205) 在 java.io.ObjectInputStream$GetFieldImpl.readFields(ObjectInputStream.java:2125) 在 java.io.ObjectInputStream.readFields(ObjectInputStream.java:536) 在 java.awt.Container.readObject(Container.java:3560) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:616) 在 java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:991) 在 java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1865) 在 java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1770) 在 java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346) 在 java.io.ObjectInputStream.readArray(ObjectInputStream.java:1684) 在 java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1340) 在 java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1963) 在 java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1887) 在 java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1770) 在 java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346) 在 java.io.ObjectInputStream.readObject(ObjectInputStream.java:368) 在 arbre.ArbreClient.getHttpObject(ArbreClient.java:92) 在 arbre.ArbreClient.init(ArbreClient.java:123) 在 sun.applet.AppletPanel.run(AppletPanel.java:436) 在 java.lang.Thread.run(Thread.java:636)
这里第 70 到 106 行是小程序本身,它尝试检索 serilaTree 对象?
` 尝试{ 结果 = 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();
}
Is there a way to ensure that the serialization of an object is done correctly?
I created an applet that I serialize with writeObject()
method and I have no Exception no error.
But when I try to de-serialize it with readObject()
I got a NullPointerException.
My applet is kinda complex but I ensured that all my fields are serializable.
The class of the objection have nested classes.
Any guess as to what could have happen?
the code below is in my servlet it creates a SerialTree .
` SerialTree st = new SerialTree() ;
try{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("/home/cof/test8"));
out.writeUnshared(st);
out.flush();
out.close();
}
Here's the stacktrace :
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)
here the lines 70 to 106 to the applet itself ,it tries to retrieve a serilaTree object ?
`
try{
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说,ser/deser 类型 Applet 是没有意义的。尝试使用不同的类。
更新:
我已阅读您的堆栈跟踪,我看到您在运行小程序时尝试反序列化小程序。我强烈怀疑这是你问题的原因。尝试将所有变量移至不同的类中(例如,将其称为 MySerializedApplet,并且不要使其扩展类 Applet),然后在此基础上进行序列化/反序列化只有一个。
To me, it doesn't make sense to ser/deser the type Applet. Try using a different class.
Update:
I have read your stacktrace, and what I see is that you tried to deserialize an applet while running an applet. I strongly suspect this to be the reason of your problem. Try moving all your variables into a different class (call it
MySerializableApplet
for example, and don't make it extend the classApplet
), then operate your serialisation/deserialization on this one only.