gpt4 book ai didi

java - 尝试将 jtable 中的数据显示到控制台时出现 ArrayIndexOutOfBoundsException 错误

转载 作者:行者123 更新时间:2023-12-01 13:39:49 25 4
gpt4 key购买 nike

我得到了ArrayIndexOutOfBoundsException当我尝试显示 data 时出错其中放入 Vector来自Jtableconsole

这里的错误:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3 >= 3
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableColumnModel.getColumn(Unknown Source)
at sun.swing.SwingUtilities2.convertColumnIndexToModel(Unknown Source)
at javax.swing.JTable.convertColumnIndexToModel(Unknown Source)
at javax.swing.JTable.getValueAt(Unknown Source)

代码如下:

ResultSet dbresultset = sqlstatement.executeQuery("select * from ***);
ResultSetMetaData rsmetadata = dbresultset.getMetaData();
int numcols = rsmetadata.getColumnCount();

if(tglbtnAdd.isSelected() == true)
{ while (dbresultset.next())
{ Vector<Object> row = new Vector<Object>(numcols);
for (int i = 1; i <= numcols; i++)
{
row.addElement( dbresultset.getObject(i) );
}
defaultmodel2.addRow(row );

}

ArrayList<String> numdata = new ArrayList<String>();
for(int count = 0; count <= table_1.getRowCount(); count++){
numdata.add(table_1.getValueAt(count, 3).toString());
}
System.out.println(numdata);

最佳答案

for(int count = 0; count <= table_1.getRowCount(); count++)

正如评论中提到的,索引范围从[0, length)。我怀疑问题是您正在使用 <=而不是< 。它可能看起来像:

for(int count = 0; count < table_1.getRowCount(); count++)

关于java - 尝试将 jtable 中的数据显示到控制台时出现 ArrayIndexOutOfBoundsException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20930443/

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