Nikhil
Nikhil

Reputation: 2883

ArrayIndexOutOfBoundsException when adding row in jtable

this.tModel = new AdvancedMibTableModel(); 
this.table = new JTable(this.tModel);
this.tModel.addRow(new Object[]{"sysLocation","1.3.6.1.2.1.1.6","0",""});

when running the above code the following exception occured.

java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableModel.justifyRows(Unknown Source)
at javax.swing.table.DefaultTableModel.insertRow(Unknown Source)
at javax.swing.table.DefaultTableModel.addRow(Unknown Source)
at javax.swing.table.DefaultTableModel.addRow(Unknown Source)

what i am doing wrong here? what is the actual problem? can anyone suggest a solution? i tried both addRow() and insertRow() but the same problem occured.

Upvotes: 2

Views: 1866

Answers (2)

mKorbel
mKorbel

Reputation: 109813

exceptions is pretty clear

at java.util.Vector.elementAt(Unknown Source)

v.s.

this.tModel.addRow(new Object[]{"sysLocation","1.3.6.1.2.1.1.6","0",""});

  • have to create Vector<Object> instead of new Object[]

  • for better help sooner post an SSCCE

Upvotes: 2

Azodious
Azodious

Reputation: 13872

Did you add the columns to Model?

If not, you should to avoid this exception.

You can add columns to model as follows:

TableColumn location = new TableColumn();
// ...
// set location fileds i.e. header etc.
// ...
this.tModel.addColumn(location); 

Upvotes: 1

Related Questions