1
votes

Hi i use a second thread to update the DefaultTableModel of a JTable each 2 seconds and aparently randomly, it throws me the following error.

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
    at java.util.Vector.elementAt(Vector.java:470)
    at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:649)
    at javax.swing.JTable.getValueAt(JTable.java:2716)
    at javax.swing.JTable.prepareRenderer(JTable.java:5714)
    at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2108)
    at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2010)
    at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1806)
    at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
    at javax.swing.JComponent.paintComponent(JComponent.java:769)
    at javax.swing.JComponent.paint(JComponent.java:1045)
    at javax.swing.JComponent.paintChildren(JComponent.java:878)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.JViewport.paint(JViewport.java:731)
    at javax.swing.JComponent.paintChildren(JComponent.java:878)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5212)
    at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1236)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5160)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4971)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:784)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706)
    at javax.swing.RepaintManager.access$1000(RepaintManager.java:62)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1651)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

The program keeps working great, but i don't know where is the fault in my code, because none of my classes appear in the stack trace.

1
Post the code that you used. Edit your question including it. - Maximin
depends of, for better help sooner post an SSCCE, short, runnable, compilable, to generating a.m. exception, otherwise everything here could be shorts to the dark, especially if is used prepareRenderer in your code - mKorbel
Which IDE you using? if it is netbeans you can view the vector class and you can get the maximum array size of that method - Ameer

1 Answers

7
votes

Welcome to the wonderful world of Event Dispatch Thread violation (and race conditions).

Basically, you should never update (directly or indirectly) any UI component from any thread other then the EDT.

Basically, when you update your TableModel, it is firing an event, which is been caught by the Table, which is trying to update itself, but the models state is in flux and does not make sense to the table...

Instead, trying using a SwingWorker to update you model, using the publish and process methods to keep updates synchronised with the EDT

Check out Concurrency in Swing for more details and examples.