0

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);
2
  • 1
    How do you know there are 3 or more columns? Commented Jan 5, 2014 at 5:29
  • That line actually assumes 4 columns. Indexes count starts at 0. I suspect OP is trying to get the value at 3rd column in which case the right call is table_1.getValueAt(count, 2). Commented Jan 5, 2014 at 5:31

1 Answer 1

1
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++)
Sign up to request clarification or add additional context in comments.

2 Comments

To try and get an object from a ResultSet the "idices" actaully start at 1, that's why the OP is using 1 and <= in the first loop. You're probably right about the second.
@peeskillet Fair enough. I shall edit my answer, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.