[英]I'm getting an Exception in Java Swing, but the program continues to run fine. Should I be worried?
I'm using a SwingWorker to update parts of the GUI, and I'm relatively new to using them so I'm not sure what's causing this or if it's important. 我正在使用SwingWorker来更新GUI的各个部分,而使用它们相对较新,因此我不确定是什么原因引起的,或者它是否很重要。 Basically, when this one big process finishes collecting data from online, the SwingWorker tells a JTable to update with the new information. 基本上,当这一大过程完成了从联机收集数据时,SwingWorker告诉JTable使用新信息进行更新。 And it does, and the GUI works exactly as it should. 而且确实如此,GUI完全可以正常工作。 However, the console prints this Exception: 但是,控制台将显示以下异常:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 19 >= 18
at java.util.Vector.elementAt(Vector.java:470)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:649)
at javax.swing.JTable.getValueAt(JTable.java:2720)
at javax.swing.JTable.prepareRenderer(JTable.java:5718)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2114)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JViewport.paint(JViewport.java:725)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1482)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1413)
at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:770)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
And I hate this Exception, because it doesn't point to anywhere in my code. 我恨这个例外,因为它并不指向我的代码的任何地方。 I know what happens right before this Exception, as I'd been trying to figure this out on my own for a while, but there's no point I can find where an ArrayOutOfBoundsError should be possible. 我知道在发生此异常之前会发生什么,因为一段时间以来我一直试图自己解决这个问题,但是我没有发现应该在哪里出现ArrayOutOfBoundsError的问题。 I'm fairly certain it has something to do with updating the JTable, but the table works fine. 我相当确定它与更新JTable有关,但是表工作正常。
So, what I'm asking is, can anyone give me an idea as to why this is happening, and, should I even be concerned, since the program still works fine? 因此,我要问的是,谁能给我一个想法,说明为什么会这样,并且,即使程序运行良好,我什至应该担心?
Edit: After extensive testing, I've determined I'm incapable of reproducing this Exception in another program. 编辑:经过广泛测试,我确定我无法在另一个程序中重现此Exception。 I've also found that it happens at a random time, but always after a call to a certain method in another class. 我还发现它发生在随机时间,但是总是在另一个类中调用某个方法之后发生。 This call is made from the process() method of the SwingWorker, and all it does is remove all rows from the DefaultTableModel, then add rows containing the new information, one at a time in a for loop. 此调用是从SwingWorker的process()方法进行的,它所做的只是从DefaultTableModel中删除所有行,然后添加包含新信息的行,一次在for循环中。 By printing to console, I've found that the Exception will be thrown before the for loops starts, after it ends, or at some random time during the for loop. 通过打印到控制台,我发现该异常将在for循环开始之前,结束之后或在for循环期间的某个随机时间抛出。 I've had the entire thing run without the Exception occurring. 我已经运行了整个程序而没有发生异常。 This a general version of the method in question: 这是所讨论方法的一般版本:
for(int i = table.getRowCount() - 1; i >= 0; i--)
model.removeRow(i);
for(InfoObject o : listOfInfoObjects) {
int[] stats = o.getStats();
model.addRow(new Object[] {o.getName(),
stats[0], stats[1], stats[2], stats[3], stats[4], stats[5]});
}
I don't know if that's any help but it's all I've got to add, sorry. 我不知道这有什么帮助,但是抱歉,这就是我要补充的全部内容。
The most likely cause of this is bad use of threading within your program. 造成这种情况的最可能原因是程序中线程的错误使用。 Swing runs a single thread called the Event Dispatch Thread and all changes to the controls, layouts, etc must be done from on that thread. Swing运行一个称为“事件调度线程”的线程,对控件,布局等的所有更改都必须在该线程上进行。
You can use SwingUtilties.invokeLater
to execute code on the EDT. 您可以使用SwingUtilties.invokeLater
在EDT上执行代码。
http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.