2014-01-06 50 views
0

我使用序列化將我的模型的當前狀態保存到文件並在幾分鐘後加載它。我的應用程序沒有任何處理JPA或類似技術的東西。這是一個完整的JavaSE桌面Swing應用程序。 序列化工作得很好。只有我在序列化之後遇到過,我爲另一個JList定義的DnD功能停止運行(DnD功能由我定義並且不是默認功能)。我不序列化GUI。我只是序列化並保存模型。每次程序運行時都會創建GUI。序列化期間不相關的NullPointerException

但曾幾何時,我發現發生以下異常,並且模型未被序列化。這種異常只發生一次。我可以在此之前保存序列化的模型(我注意到代碼中沒有發生任何變化)它在我的代碼中沒有任何線索。其中,該拋出異常的代碼行是:

out.writeObject(galMachine); 

我知道錯誤是那麼晦澀難懂,但我明白任何線索。謝謝!

順便說一句,galMachine不爲null。該程序運行良好,所有成員都已正確初始化!

注意:只有當我已經將序列化的實例加載到程序中而不是它自己創建了所有對象時,纔會發生此異常。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at javax.swing.plaf.metal.MetalScrollBarUI.installDefaults(MetalScrollBarUI.java:90) 
    at javax.swing.plaf.basic.BasicScrollBarUI.installUI(BasicScrollBarUI.java:168) 
    at javax.swing.JScrollBar.writeObject(JScrollBar.java:771) 
    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:601) 
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:975) 
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480) 
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416) 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174) 
    at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1362) 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1170) 
    at java.io.ObjectOutputStream.access$300(ObjectOutputStream.java:161) 
    at java.io.ObjectOutputStream$PutFieldImpl.writeFields(ObjectOutputStream.java:1687) 
    at java.io.ObjectOutputStream.writeFields(ObjectOutputStream.java:478) 
    at java.awt.Container.writeObject(Container.java:3681) 
    at sun.reflect.GeneratedMethodAccessor23.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:975) 
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480) 
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416) 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174) 
    at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1362) 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1170) 
    at java.io.ObjectOutputStream.access$300(ObjectOutputStream.java:161) 
    at java.io.ObjectOutputStream$PutFieldImpl.writeFields(ObjectOutputStream.java:1687) 
    at java.io.ObjectOutputStream.writeFields(ObjectOutputStream.java:478) 
    at java.awt.Container.writeObject(Container.java:3681) 
    at sun.reflect.GeneratedMethodAccessor23.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:975) 
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480) 
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416) 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174) 
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528) 
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493) 
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416) 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174) 
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346) 
    at java.util.ArrayList.writeObject(ArrayList.java:710) 
    at sun.reflect.GeneratedMethodAccessor19.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:975) 
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480) 
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416) 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174) 
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528) 
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493) 
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416) 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174) 
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346) 
    at View.AppJFrame.saveMenuItemActionPerformed(AppJFrame.java:430) 
    at View.AppJFrame.access$500(AppJFrame.java:51) 
    at View.AppJFrame$4.actionPerformed(AppJFrame.java:307) 
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) 
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) 
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376) 
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833) 
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877) 
    at java.awt.Component.processMouseEvent(Component.java:6505) 
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) 
    at java.awt.Component.processEvent(Component.java:6270) 
    at java.awt.Container.processEvent(Container.java:2229) 
    at java.awt.Component.dispatchEventImpl(Component.java:4861) 
    at java.awt.Container.dispatchEventImpl(Container.java:2287) 
    at java.awt.Component.dispatchEvent(Component.java:4687) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) 
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) 
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) 
    at java.awt.Container.dispatchEventImpl(Container.java:2273) 
    at java.awt.Window.dispatchEventImpl(Window.java:2719) 
    at java.awt.Component.dispatchEvent(Component.java:4687) 
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723) 
    at java.awt.EventQueue.access$200(EventQueue.java:103) 
    at java.awt.EventQueue$3.run(EventQueue.java:682) 
    at java.awt.EventQueue$3.run(EventQueue.java:680) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) 
    at java.awt.EventQueue$4.run(EventQueue.java:696) 
    at java.awt.EventQueue$4.run(EventQueue.java:694) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97) 

我使用Java 1.7與大多使用Netbeans 7.2 GUI生成器創建的GUI。我沒有Netbeans的特定功能的應用程序和第一線是指下列方法在Java代碼的第一行:

protected void installDefaults() { 
    scrollBarWidth = ((Integer)(UIManager.get("ScrollBar.width"))).intValue(); 
    super.installDefaults(); 
    bumps = new MetalBumps(10, 10, thumbHighlightColor, thumbShadow, thumbColor); 
} 

我再次指出,我有沒有興趣去挽救GUI和我在我的模型(galMachine)中沒有任何GUI對象的參考。直接針對GUI的所有重要事項都是兩個AbstractListModel對象,它們沒有提及其組件JList。我只序列化galMachine


我發現了一個類似的異常也出現在的DragDrop在被覆蓋的importData方法內。它發生在第一線試

try { 

    Object obj = ts.getTransferable().getTransferData(TransferableProductJob.PRODUCTJOB_DATA_FLAVOR); 
    if(obj instanceof ProductJob){ 

     ProductJob pj = (ProductJob) obj; 
     JList.DropLocation dl = (JList.DropLocation) ts.getDropLocation(); 
     int idx = dl.getIndex(); 
     StoreTank tank = ((StoreTank) jl.getModel().getElementAt(idx)); 
     if(tank.getCurrentJobStage() != null){ 
      JOptionPane.showMessageDialog(null,"\"" + tank.getDescription() + "\" è occupato."); 
      return false; 
     } 
     tank.setReserved(true); 
      tank.setReserved(false); 
      return false; 
     int initialTankNumber = tank.getNumber(); 
     pj.importToStroreTank(initialTankNumber); 
     galvanicMachine.addJob(pj); 
     pj.start(); 
    } 
} catch (UnsupportedFlavorException ex) { 
    Logger.getLogger(WaitingListJPanel.class.getName()).log(Level.SEVERE, null, ex); 
} catch (IOException ex) { 
    Logger.getLogger(WaitingListJPanel.class.getName()).log(Level.SEVERE, null, ex); 
} catch (Exception ex){ 
    Logger.getLogger(WaitingListJPanel.class.getName()).log(Level.SEVERE, null, ex); 
} 

java.lang.NullPointerException 
    at javax.swing.plaf.metal.MetalScrollBarUI.installDefaults(MetalScrollBarUI.java:90) 
    at javax.swing.plaf.basic.BasicScrollBarUI.installUI(BasicScrollBarUI.java:168) 
    at javax.swing.JScrollBar.writeObject(JScrollBar.java:771) 
    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:601) 
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:975) 
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480) 
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416) 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174) 
    at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1362) 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1170) 
    at java.io.ObjectOutputStream.access$300(ObjectOutputStream.java:161) 
    at java.io.ObjectOutputStream$PutFieldImpl.writeFields(ObjectOutputStream.java:1687) 
    at java.io.ObjectOutputStream.writeFields(ObjectOutputStream.java:478) 
    at java.awt.Container.writeObject(Container.java:3681) 
    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:601) 
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:975) 
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480) 
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416) 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174) 
    at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1362) 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1170) 
    at java.io.ObjectOutputStream.access$300(ObjectOutputStream.java:161) 
    at java.io.ObjectOutputStream$PutFieldImpl.writeFields(ObjectOutputStream.java:1687) 
    at java.io.ObjectOutputStream.writeFields(ObjectOutputStream.java:478) 
    at java.awt.Container.writeObject(Container.java:3681) 
    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:601) 
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:975) 
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480) 
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416) 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174) 
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528) 
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493) 
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416) 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174) 
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346) 
    at java.util.ArrayList.writeObject(ArrayList.java:710) 
    at sun.reflect.GeneratedMethodAccessor12.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:975) 
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480) 
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416) 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174) 
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528) 
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493) 
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416) 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174) 
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528) 
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493) 
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416) 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174) 
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346) 
    at sun.awt.datatransfer.TransferableProxy.getTransferData(TransferableProxy.java:83) 
    at java.awt.dnd.DropTargetContext$TransferableProxy.getTransferData(DropTargetContext.java:376) 
    at View.panels.WaitingListJPanel$JListTransferHandler.importData(WaitingListJPanel.java:240) 
    at javax.swing.TransferHandler$DropHandler.drop(TransferHandler.java:1536) 
    at java.awt.dnd.DropTarget.drop(DropTarget.java:450) 
    at javax.swing.TransferHandler$SwingDropTarget.drop(TransferHandler.java:1274) 
    at sun.awt.dnd.SunDropTargetContextPeer.processDropMessage(SunDropTargetContextPeer.java:537) 
    at sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.dispatchDropEvent(SunDropTargetContextPeer.java:851) 
    at sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.dispatchEvent(SunDropTargetContextPeer.java:775) 
    at sun.awt.dnd.SunDropTargetEvent.dispatch(SunDropTargetEvent.java:48) 
    at java.awt.Component.dispatchEventImpl(Component.java:4716) 
    at java.awt.Container.dispatchEventImpl(Container.java:2287) 
    at java.awt.Component.dispatchEvent(Component.java:4687) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) 
    at java.awt.LightweightDispatcher.processDropTargetEvent(Container.java:4566) 
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4417) 
    at java.awt.Container.dispatchEventImpl(Container.java:2273) 
    at java.awt.Window.dispatchEventImpl(Window.java:2719) 
    at java.awt.Component.dispatchEvent(Component.java:4687) 
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723) 
    at java.awt.EventQueue.access$200(EventQueue.java:103) 
    at java.awt.EventQueue$3.run(EventQueue.java:682) 
    at java.awt.EventQueue$3.run(EventQueue.java:680) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) 
    at java.awt.EventQueue$4.run(EventQueue.java:696) 
    at java.awt.EventQueue$4.run(EventQueue.java:694) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97) 

我在網上搜索,我發現了這樣這個錯誤已經發生,並作爲一個bug之前只有一次公佈,並已被否決後的bug。這是the bug report。但它不能在我的代碼中,因爲我不試圖在兩個線程中同時初始化兩個JScrollPane,但我希望它能幫助建議解決方案。

+2

我們將需要更多的代碼比這個要弄清楚你的例外。 – crush

+0

@crush JA!大約有20000行代碼,但你可以引導請哪一部分是有用的? – Johnny

+0

讓我們從堆棧跟蹤引用的代碼開始 – crush

回答

2

我發現了一個非常痛苦的補救措施。它應該是Java中的一個錯誤。當我將nAbus的lookAndFeel更改爲系統默認或金屬時,問題消失。所以這是Java中的一個錯誤! :(

UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");