有沒有一種方法可以確保對象的序列化是否正確完成? 我創建了一個小程序,我用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對象?
` 嘗試{ 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();
}
如何發佈你的stacktrace? – Vincent 2011-05-13 15:38:19
你應該檢查你設定的所有字段是否已經初始化。這是NullPointerException的最常見原因。 – Osiris76 2011-05-13 15:44:26
我不這麼認爲。我試着用品牌設置爲無效的對象「汽車」,它完美的作品。關於堆棧跟蹤:我在readObject()方法上得到一個NullPointerException。 – lemoos 2011-05-13 16:06:15