2012-01-06 64 views
1

基本上,我寫了一個程序,它將形狀繪製到屏幕上,並將每個形狀保存到ArrayList中。我想要做的是弄清楚如何將ArrayList保存到一個文件中,這樣我可以稍後再調用它並編輯已經存在的形狀。NotSerializableException

所以我一直有一些麻煩,搞清楚爲什麼我一直在得到一個NotSerializableException,當我已經對象形式可序列化。

下面是我的代碼和Shape對象的保存部分。

save.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e){ 
    int returnVal = fc.showSaveDialog(BallWorld.this); 
    if(returnVal == JFileChooser.APPROVE_OPTION){ 
     File file = fc.getSelectedFile(); 
     ArrayList list = bp.shapes; 
     FileOutputStream fos = null; 
     ObjectOutputStream oos = null; 
     try{ 
     fos = new FileOutputStream(file); 
     oos = new ObjectOutputStream(fos); 
     oos.writeObject(list); 
     oos.close(); 
     } 
     catch(IOException ex){ 
     ex.printStackTrace(); 
     } 
    } 
    } 
}); 

public class Shape implements Serializable{ 
    int radius; 
    Point center; 
    Color color; 
    int sides; 
    Polygon shape; 
    public Shape(int _radius, Point _center, Color _color, int _sides){ 
     radius = _radius; 
     center = _center; 
     color = _color; 
     sides = _sides; 
    } 
    public void draw(Graphics g){ 
     Graphics2D g2d = (Graphics2D) g; 
     g2d.setColor(color); 
     shape = new Polygon(); 
     if(sides != 0 && sides != 4){ 
     if(sides % 2 == 0){ 
      if(sides/2 % 2 == 0){ 
      for(int i = 0; i < sides; i++){ 
       shape.addPoint((int) (center.x + radius * Math.cos(i * 2 * Math.PI/sides - Math.PI/sides)), (int) (center.y + radius * Math.sin(i * 2 * Math.PI/sides - Math.PI/sides))); 
      } 
      } 
      else if(sides/2 % 1 == 0){ 
      for(int i = 0; i < sides; i++){ 
       shape.addPoint((int) (center.x + radius * Math.cos(i * 2 * Math.PI/sides)), (int) (center.y + radius * Math.sin(i * 2 * Math.PI/sides))); 
      } 
      } 
     } 
     else{ 
      for(int i = 0; i < sides; i++){ 
      shape.addPoint((int) (center.x + radius * Math.cos(i * 2 * Math.PI/sides - Math.PI/2)), (int) (center.y + radius * Math.sin(i * 2 * Math.PI/sides - Math.PI/2))); 
      } 
     } 
     g2d.fillPolygon(shape); 
     } 
     else if(sides == 4){ 
     g2d.fillRect(center.x - radius, center.y - radius, radius * 2, radius * 2); 
     } 
     else{ 
     g2d.fillOval(center.x - radius, center.y - radius, radius * 2, radius * 2); 
     } 
     repaint(); 
    } 
    public boolean contains(Point p){ 
     if(sides == 0){ 
     if(center.distance(p) <= radius){ 
      return true; 
     } 
     } 
     else if(sides == 4){ 
     if(p.x <= center.x + radius && p.x >= center.x - radius && p.y <= center.y + radius && p.y >= center.y - radius){ 
      return true; 
     } 
     } 
     else{ 
     if(shape.contains(p)){ 
      return true; 
     } 
     } 
     return false; 
    } 
    } 

java.io.NotSerializableException: javax.swing.plaf.basic.BasicFileChooserUI$AcceptAllFileFilter 
at java.io.ObjectOutputStream.writeObject0(Unknown Source) 
at java.io.ObjectOutputStream.writeArray(Unknown Source) 
at java.io.ObjectOutputStream.writeObject0(Unknown Source) 
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source) 
at java.io.ObjectOutputStream.writeSerialData(Unknown Source) 
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) 
at java.io.ObjectOutputStream.writeObject0(Unknown Source) 
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source) 
at java.io.ObjectOutputStream.defaultWriteObject(Unknown Source) 
at javax.swing.JComboBox.writeObject(Unknown Source) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source) 
at java.io.ObjectOutputStream.writeSerialData(Unknown Source) 
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) 
at java.io.ObjectOutputStream.writeObject0(Unknown Source) 
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source) 
at java.io.ObjectOutputStream.defaultWriteObject(Unknown Source) 
at javax.swing.JLabel.writeObject(Unknown Source) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source) 
at java.io.ObjectOutputStream.writeSerialData(Unknown Source) 
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) 
at java.io.ObjectOutputStream.writeObject0(Unknown Source) 
at java.io.ObjectOutputStream.writeArray(Unknown Source) 
at java.io.ObjectOutputStream.writeObject0(Unknown Source) 
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source) 
at java.io.ObjectOutputStream.writeSerialData(Unknown Source) 
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) 
at java.io.ObjectOutputStream.writeObject0(Unknown Source) 
at java.io.ObjectOutputStream.writeObject(Unknown Source) 
at javax.swing.ArrayTable.writeArrayTable(Unknown Source) 
at javax.swing.JComponent.writeObject(Unknown Source) 
at sun.reflect.GeneratedMethodAccessor11.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source) 
at java.io.ObjectOutputStream.writeSerialData(Unknown Source) 
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) 
at java.io.ObjectOutputStream.writeObject0(Unknown Source) 
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source) 
at java.io.ObjectOutputStream.writeSerialData(Unknown Source) 
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) 
at java.io.ObjectOutputStream.writeObject0(Unknown Source) 
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source) 
at java.io.ObjectOutputStream.defaultWriteObject(Unknown Source) 
at java.awt.Window.writeObject(Unknown Source) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source) 
at java.io.ObjectOutputStream.writeSerialData(Unknown Source) 
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) 
at java.io.ObjectOutputStream.writeObject0(Unknown Source) 
at java.io.ObjectOutputStream.writeObject(Unknown Source) 
at java.awt.Window.writeObject(Unknown Source) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source) 
at java.io.ObjectOutputStream.writeSerialData(Unknown Source) 
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) 
at java.io.ObjectOutputStream.writeObject0(Unknown Source) 
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source) 
at java.io.ObjectOutputStream.writeSerialData(Unknown Source) 
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) 
at java.io.ObjectOutputStream.writeObject0(Unknown Source) 
at java.io.ObjectOutputStream.writeObject(Unknown Source) 
at java.util.ArrayList.writeObject(Unknown Source) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source) 
at java.io.ObjectOutputStream.writeSerialData(Unknown Source) 
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) 
at java.io.ObjectOutputStream.writeObject0(Unknown Source) 
at java.io.ObjectOutputStream.writeObject(Unknown Source) 
at BallWorld$2.actionPerformed(BallWorld.java:112) 
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
at javax.swing.AbstractButton.doClick(Unknown Source) 
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source) 
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source) 
at java.awt.Component.processMouseEvent(Unknown Source) 
at javax.swing.JComponent.processMouseEvent(Unknown Source) 
at java.awt.Component.processEvent(Unknown Source) 
at java.awt.Container.processEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Window.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$000(Unknown Source) 
at java.awt.EventQueue$1.run(Unknown Source) 
at java.awt.EventQueue$1.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue$2.run(Unknown Source) 
at java.awt.EventQueue$2.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.run(Unknown Source) 
+2

確保你所有的屬性形狀對象序列化太像點,顏色等.. – 2012-01-06 21:49:59

+0

我已經檢查的所有這些類的,和他們都是可序列化的 – lordraiden15 2012-01-06 21:51:11

+0

你看看這個:http://stackoverflow.com/questions/678481/cannot-serialize-deserialize-arraylist – 2012-01-06 21:54:03

回答

3

根據您正在爲repaint()在外形的號召,我猜,這是在內部類。因此它有一個引用其封閉實例的隱藏變量。它會嘗試將該對象與自身一起序列化。外部類是可序列化的嗎?或者更好的是,Shape可以變成一個普通的頂級課程嗎?

1

爲了避免NotSerializableException確保:

  1. 類實現Serializable
  2. 所有非原始成員實現Serializable接口(或者是短暫的,而不是)
  3. 如果你的類是內部類它是靜態的或外部類實現 可序列化的

除此之外,還需要爲每個Serializable類定義serialVersionUID。檢查三種情況下都上面加:

  1. 所有序列化
  2. 如果你的類是匿名類,定義它有太多

注:您的代碼可能沒有的serialVersionUID有時候運行但read the last paragraph in Serializable's javadoc瞭解爲什麼它會成爲一個問題,取決於環境。


有一個虛擬機選項可用於向異常添加詳細信息。它顯示了根和嵌套類不能序列化:

-Dsun.io.serialization.extendedDebugInfo=true 
相關問題