Reputation: 161
I got ArrayIndexOutOfBoundsException
error when I try to display data
which put into Vector
from Jtable
to the console
Here the error:
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)
Here the code:
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);
Upvotes: 0
Views: 1140
Reputation: 11939
for(int count = 0; count <= table_1.getRowCount(); count++)
As mentioned in the comments, indexes range from [0, length). I suspect the problem is that you are using <=
instead of <
. It should probably look something like:
for(int count = 0; count < table_1.getRowCount(); count++)
Upvotes: 1