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