2011-08-29 64 views
0

我不知道爲什麼,但是這個代碼導致的StackOverflowError:更新JList的模型引起的StackOverflowError

public void update() { 
    DefaultListModel lm = new DefaultListModel(); 
    if (elements != null) 
    for (String elem : elements) { 
     lm.addElement(lm); 
    } 
    jList1.setModel(lm); 
    repaint(); 
    } 

我試圖調試這一點,實際上的StackOverflowError外發生(我想在AWT事件隊列.... ),這裏是我在控制檯中看到:

首先這個多次:

at javax.swing.DefaultListModel.toString(DefaultListModel.java:397) 
    at java.lang.String.valueOf(String.java:2826) 
    at java.lang.StringBuilder.append(StringBuilder.java:115) 
    at java.util.AbstractCollection.toString(AbstractCollection.java:422) 
    at java.util.Vector.toString(Vector.java:940) 
    at javax.swing.DefaultListModel.toString(DefaultListModel.java:397) 
    at java.lang.String.valueOf(String.java:2826) 

然後重複這個(都在同一個堆棧跟蹤):

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at javax.swing.SizeRequirements.calculateAlignedPositions(SizeRequirements.java:444) 
    at javax.swing.BoxLayout.layoutContainer(BoxLayout.java:415) 
    at java.awt.Container.layout(Container.java:1421) 
    at java.awt.Container.doLayout(Container.java:1410) 
    at java.awt.Container.validateTree(Container.java:1507) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validate(Container.java:1480) 
    at java.awt.Window.dispatchEventImpl(Window.java:2476) 
    at java.awt.Component.dispatchEvent(Component.java:4460) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at javax.swing.SizeRequirements.calculateAlignedPositions(SizeRequirements.java:444) 
    at javax.swing.BoxLayout.layoutContainer(BoxLayout.java:415) 
    at java.awt.Container.layout(Container.java:1421) 
    at java.awt.Container.doLayout(Container.java:1410) 
    at java.awt.Container.validateTree(Container.java:1507) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validate(Container.java:1480) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.ensureCurrentLayout(BasicTabbedPaneUI.java:1429) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.paint(BasicTabbedPaneUI.java:770) 
    at javax.swing.plaf.ComponentUI.update(ComponentUI.java:143) 
    at javax.swing.JComponent.paintComponent(JComponent.java:752) 
    at javax.swing.JComponent.paint(JComponent.java:1029) 
    at javax.swing.JComponent.paintChildren(JComponent.java:862) 
    at javax.swing.JComponent.paint(JComponent.java:1038) 
    at javax.swing.JComponent.paintChildren(JComponent.java:862) 
    at javax.swing.JComponent.paint(JComponent.java:1038) 
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:567) 
    at javax.swing.JComponent.paintChildren(JComponent.java:862) 
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131) 
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1479) 
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1410) 
    at javax.swing.RepaintManager.paint(RepaintManager.java:1224) 
    at javax.swing.JComponent.paint(JComponent.java:1015) 
    at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21) 
    at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60) 
    at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97) 
    at java.awt.Container.paint(Container.java:1780) 
    at java.awt.Window.paint(Window.java:3375) 
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796) 
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713) 
    at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693) 
    at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125) 
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at javax.swing.SizeRequirements.calculateAlignedPositions(SizeRequirements.java:444) 
    at javax.swing.BoxLayout.layoutContainer(BoxLayout.java:415) 
    at java.awt.Container.layout(Container.java:1421) 
    at java.awt.Container.doLayout(Container.java:1410) 
    at java.awt.Container.validateTree(Container.java:1507) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validate(Container.java:1480) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.ensureCurrentLayout(BasicTabbedPaneUI.java:1429) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.paint(BasicTabbedPaneUI.java:770) 
    at javax.swing.plaf.ComponentUI.update(ComponentUI.java:143) 
    at javax.swing.JComponent.paintComponent(JComponent.java:752) 
    at javax.swing.JComponent.paint(JComponent.java:1029) 
    at javax.swing.JComponent.paintChildren(JComponent.java:862) 
    at javax.swing.JComponent.paint(JComponent.java:1038) 
    at javax.swing.JComponent.paintChildren(JComponent.java:862) 
    at javax.swing.JComponent.paint(JComponent.java:1038) 
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:567) 
    at javax.swing.JComponent.paintChildren(JComponent.java:862) 
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131) 
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1479) 
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1410) 
    at javax.swing.RepaintManager.paint(RepaintManager.java:1224) 
    at javax.swing.JComponent.paint(JComponent.java:1015) 
    at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21) 
    at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60) 
    at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97) 
    at java.awt.Container.paint(Container.java:1780) 
    at java.awt.Window.paint(Window.java:3375) 
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796) 
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713) 
    at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693) 
    at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125) 
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at javax.swing.SizeRequirements.calculateAlignedPositions(SizeRequirements.java:444) 
    at javax.swing.BoxLayout.layoutContainer(BoxLayout.java:415) 
    at java.awt.Container.layout(Container.java:1421) 
    at java.awt.Container.doLayout(Container.java:1410) 
    at java.awt.Container.validateTree(Container.java:1507) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validate(Container.java:1480) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.ensureCurrentLayout(BasicTabbedPaneUI.java:1429) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.getTabBounds(BasicTabbedPaneUI.java:1449) 
    at com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI.setRolloverTab(WindowsTabbedPaneUI.java:86) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneLayout.layoutContainer(BasicTabbedPaneUI.java:2384) 
    at java.awt.Container.layout(Container.java:1421) 
    at java.awt.Container.doLayout(Container.java:1410) 
    at java.awt.Container.validateTree(Container.java:1507) 
    at java.awt.Container.validate(Container.java:1480) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.ensureCurrentLayout(BasicTabbedPaneUI.java:1429) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.getTabBounds(BasicTabbedPaneUI.java:1449) 
    at com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI.setRolloverTab(WindowsTabbedPaneUI.java:89) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.setRolloverTab(BasicTabbedPaneUI.java:558) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.access$2000(BasicTabbedPaneUI.java:37) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI$Handler.mouseMoved(BasicTabbedPaneUI.java:3645) 
    at java.awt.Component.processMouseMotionEvent(Component.java:6312) 
    at javax.swing.JComponent.processMouseMotionEvent(JComponent.java:3285) 
    at java.awt.Component.processEvent(Component.java:6036) 
    at java.awt.Container.processEvent(Container.java:2041) 
    at java.awt.Component.dispatchEventImpl(Component.java:4630) 
    at java.awt.Container.dispatchEventImpl(Container.java:2099) 
    at java.awt.Component.dispatchEvent(Component.java:4460) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577) 
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4251) 
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) 
    at java.awt.Container.dispatchEventImpl(Container.java:2085) 
    at java.awt.Window.dispatchEventImpl(Window.java:2478) 
    at java.awt.Component.dispatchEvent(Component.java:4460) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at javax.swing.SizeRequirements.calculateAlignedPositions(SizeRequirements.java:444) 
    at javax.swing.BoxLayout.layoutContainer(BoxLayout.java:415) 
    at java.awt.Container.layout(Container.java:1421) 
    at java.awt.Container.doLayout(Container.java:1410) 
    at java.awt.Container.validateTree(Container.java:1507) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validate(Container.java:1480) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.ensureCurrentLayout(BasicTabbedPaneUI.java:1429) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.getTabBounds(BasicTabbedPaneUI.java:1449) 
    at com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI.setRolloverTab(WindowsTabbedPaneUI.java:86) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneLayout.layoutContainer(BasicTabbedPaneUI.java:2384) 
    at java.awt.Container.layout(Container.java:1421) 
    at java.awt.Container.doLayout(Container.java:1410) 
    at java.awt.Container.validateTree(Container.java:1507) 
    at java.awt.Container.validate(Container.java:1480) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.ensureCurrentLayout(BasicTabbedPaneUI.java:1429) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.getTabBounds(BasicTabbedPaneUI.java:1449) 
    at com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI.setRolloverTab(WindowsTabbedPaneUI.java:89) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.setRolloverTab(BasicTabbedPaneUI.java:558) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.access$2000(BasicTabbedPaneUI.java:37) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI$Handler.mouseMoved(BasicTabbedPaneUI.java:3645) 
    at java.awt.Component.processMouseMotionEvent(Component.java:6312) 
    at javax.swing.JComponent.processMouseMotionEvent(JComponent.java:3285) 
    at java.awt.Component.processEvent(Component.java:6036) 
    at java.awt.Container.processEvent(Container.java:2041) 
    at java.awt.Component.dispatchEventImpl(Component.java:4630) 
    at java.awt.Container.dispatchEventImpl(Container.java:2099) 
    at java.awt.Component.dispatchEvent(Component.java:4460) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577) 
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4251) 
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) 
    at java.awt.Container.dispatchEventImpl(Container.java:2085) 
    at java.awt.Window.dispatchEventImpl(Window.java:2478) 
    at java.awt.Component.dispatchEvent(Component.java:4460) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at javax.swing.SizeRequirements.calculateAlignedPositions(SizeRequirements.java:444) 
    at javax.swing.BoxLayout.layoutContainer(BoxLayout.java:415) 
    at java.awt.Container.layout(Container.java:1421) 
    at java.awt.Container.doLayout(Container.java:1410) 
    at java.awt.Container.validateTree(Container.java:1507) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validateTree(Container.java:1513) 
    at java.awt.Container.validate(Container.java:1480) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.ensureCurrentLayout(BasicTabbedPaneUI.java:1429) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.getTabBounds(BasicTabbedPaneUI.java:1449) 
    at com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI.setRolloverTab(WindowsTabbedPaneUI.java:86) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneLayout.layoutContainer(BasicTabbedPaneUI.java:2384) 
    at java.awt.Container.layout(Container.java:1421) 
    at java.awt.Container.doLayout(Container.java:1410) 
    at java.awt.Container.validateTree(Container.java:1507) 
    at java.awt.Container.validate(Container.java:1480) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.ensureCurrentLayout(BasicTabbedPaneUI.java:1429) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.getTabBounds(BasicTabbedPaneUI.java:1449) 
    at com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI.setRolloverTab(WindowsTabbedPaneUI.java:89) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.setRolloverTab(BasicTabbedPaneUI.java:558) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI.access$2000(BasicTabbedPaneUI.java:37) 
    at javax.swing.plaf.basic.BasicTabbedPaneUI$Handler.mouseMoved(BasicTabbedPaneUI.java:3645) 
    at java.awt.Component.processMouseMotionEvent(Component.java:6312) 
    at javax.swing.JComponent.processMouseMotionEvent(JComponent.java:3285) 
    at java.awt.Component.processEvent(Component.java:6036) 
    at java.awt.Container.processEvent(Container.java:2041) 
    at java.awt.Component.dispatchEventImpl(Component.java:4630) 
    at java.awt.Container.dispatchEventImpl(Container.java:2099) 
    at java.awt.Component.dispatchEvent(Component.java:4460) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577) 
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4251) 
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) 
    at java.awt.Container.dispatchEventImpl(Container.java:2085) 
    at java.awt.Window.dispatchEventImpl(Window.java:2478) 
    at java.awt.Component.dispatchEvent(Component.java:4460) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 
Exception in thread "AWT-EventQueue-0" Exception in thread "AWT-EventQueue-0" 

任何想法?

+0

我沒有看到一個StackOverflowError,只是重複的NPE,它來自線程不斷嘗試'calculateAlignedPositions'。 – Thomas

+0

SORRY MATES,真是愚蠢的錯誤,刪除問題 – gotch4

回答

3

您要添加的列表作爲元素本身,在無限遞歸過程的結果,當您嘗試遞歸地將其轉換爲一個字符串:

lm.addElement(lm); 

你大概意思是:

lm.addElement(elem); 

修復此問題後,NullPointerException可能會消失,或者它們可能完全無關。