2011-05-13 54 views
1

有沒有一種方法可以確保對象的序列化是否正確完成? 我創建了一個小程序,我用writeObject()方法序列化,並且我沒有異常沒有錯誤。 但是當我試圖用readObject()對它進行反序列化時,我得到了一個N​​ullPointerException。 我的小程序有點複雜,但我確保所有的字段都是可序列化的。確保對象序列化正確完成

異議的類有嵌套類。

任何可能發生的猜測?

下面的代碼是在我的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(); 
} 
+1

如何發佈你的stacktrace? – Vincent 2011-05-13 15:38:19

+0

你應該檢查你設定的所有字段是否已經初始化。這是NullPointerException的最常見原因。 – Osiris76 2011-05-13 15:44:26

+0

我不這麼認爲。我試着用品牌設置爲無效的對象「汽車」,它完美的作品。關於堆棧跟蹤:我在readObject()方法上得到一個NullPointerException。 – lemoos 2011-05-13 16:06:15

回答

6

對我來說,ser/deser類型的Applet沒有意義。嘗試使用不同的課程。

更新: 我讀過你的堆棧跟蹤,和我所看到的是,你試圖反序列化的小應用程序運行小。我強烈懷疑這是你問題的原因。嘗試將所有變量移動到不同的類中(例如,將其稱爲MySerializableApplet,並且不要將其擴展爲類Applet),然後僅對此類序列化/反序列化進行操作。

+0

當你只在服務器上有資源時,它是有意義的,所以你所能做的就是在服務器端創建這個對象,並將它發送給你的客戶端來創建你的applet。 – lemoos 2011-05-13 15:48:17

+0

我在想瀏覽器負責實例化Applets。從哪裏反序列化小程序?在瀏覽器中?從哪個班級? – Vincent 2011-05-13 16:21:28

+0

我沒有在瀏覽器上測試,我在我的IDE(Eclispe)中。當我嘗試反序列化我的對象時發生NullPointerException。 – lemoos 2011-05-16 08:21:52

相關問題