确保对象的序列化操作正确(Ensuring that the serialization of an

2019-09-28 19:36发布

有没有一种方法,以确保对象的序列化是否正确呢? 我创建的我与序列化的小应用程序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 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)

这里的线70到106的小应用程序本身,它试图获取一个serilaTree对象?

`尝试{结果=新的ObjectInputStream(新的FileInputStream( “/家/ 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();
 }

Answer 1:

对我来说,它没有任何意义SER / DESER类型的小程序。 尝试使用不同的类。

更新:我读过你的堆栈跟踪,和我所看到的是,你试图反序列化的小应用程序运行小。 我强烈怀疑这是你的问题的原因。 尝试所有的变量移动到不同的等级(称之为MySerializableApplet例如,不要让它扩展类Applet ),那么仅这一项操作你的序列化/反序列化。



文章来源: Ensuring that the serialization of an object is done correctly