gpt4 book ai didi

java - 为什么我在线程 "AWT-EventQueue-0"java.lang.ArrayIndexOutOfBoundsException : 2? 中收到异常

转载 作者:行者123 更新时间:2023-12-01 05:29:24 25 4
gpt4 key购买 nike

每次尝试更新 JTable 中的数据时都会收到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2
at javax.swing.DefaultRowSorter.convertRowIndexToModel(DefaultRowSorter.java:501)
at javax.swing.JTable.convertRowIndexToModel(JTable.java:2620)
at javax.swing.JTable.getValueAt(JTable.java:2695)
at javax.swing.JTable.prepareRenderer(JTable.java:5712)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2069)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1971)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1767)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:143)
at javax.swing.JComponent.paintComponent(JComponent.java:751)
at javax.swing.JComponent.paint(JComponent.java:1017)
at javax.swing.JComponent.paintChildren(JComponent.java:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JViewport.paint(JViewport.java:747)
at javax.swing.JComponent.paintChildren(JComponent.java:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JComponent.paintChildren(JComponent.java:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JComponent.paintChildren(JComponent.java:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JComponent.paintChildren(JComponent.java:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5112)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:278)
at javax.swing.RepaintManager.paint(RepaintManager.java:1220)
at javax.swing.JComponent._paintImmediately(JComponent.java:5060)
at javax.swing.JComponent.paintImmediately(JComponent.java:4870)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:803)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:694)
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)

数据似乎更新得很好,但出了点问题,它甚至没有引用我的任何代码,这让我很困扰。经过一段时间的调试,我确定以下几行代码导致了问题:

public class LoadingLogListThread extends Thread{

...

public void run() {

...
int colNo = rsmd.getColumnCount();

while(rs.next() ){
Object[] objects = new Object[colNo];
for(int i=0;i<colNo;i++){
objects[i]=rs.getObject(i+1);
}
if( objects != null )
aModel.addRow(objects);
count++;
}

mainView.logEntryTable.setModel(aModel);
...

}

当我单击按钮来过滤列表时,线程就会创建并启动,然后返回到数据库以获取可能超出已获取数据的更多数据。我计划稍后确定是否需要新的获取,并且仅在这种情况下执行上述操作,否则仅过滤已获取的结果。

最佳答案

您需要更新事件调度线程上的模型。您生成一个新线程来执行某些工作是正确的,特别是因为正在从数据库中获取数据。试试这个:

public class LoadingLogListThread extends Thread{

...

public void run() {

...
int colNo = rsmd.getColumnCount();

while(rs.next() ){
Object[] objects = new Object[colNo];
for(int i=0;i<colNo;i++){
objects[i]=rs.getObject(i+1);
}
if( objects != null )
aModel.addRow(objects);
count++;
}

SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
mainView.logEntryTable.setModel(aModel);
}

});

...

}

关于java - 为什么我在线程 "AWT-EventQueue-0"java.lang.ArrayIndexOutOfBoundsException : 2? 中收到异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9237544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com