Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayIndexOutOfBounds Exception  RSS feed

khad M
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a JTable with 3 coulumns and 4 rows. I have also added a button which when clicked removes the selected row.
Now the problem is, if i select the last row and press the button the row gets deleted. But after this if i select any other row it throws an ArrayIndexOutOfBoundsException.
The following is the stact trace i got for the exception
Exception occurred during event dispatching:
java.lang.ArrayIndexOutOfBoundsException: 4 >= 4
at java.util.Vector.elementAt(
at javax.swing.table.DefaultTableModel.setValueAt(
at javax.swing.JTable.setValueAt(
at javax.swing.JTable.editingStopped(
at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.
at javax.swing.AbstractCellEditor.stopCellEditing(AbstractCellEditor.jav
at javax.swing.JTable.editCellAt(
at javax.swing.plaf.basic.BasicTableUI$MouseInputHandler.mousePressed(Ba
at java.awt.AWTEventMulticaster.mousePressed(
at java.awt.AWTEventMulticaster.mousePressed(
at java.awt.Component.processMouseEvent(
at java.awt.Component.processEvent(
at java.awt.Container.processEvent(
at java.awt.Component.dispatchEventImpl(
at java.awt.Container.dispatchEventImpl(
at java.awt.Component.dispatchEvent(
at java.awt.LightweightDispatcher.retargetMouseEvent(
at java.awt.LightweightDispatcher.processMouseEvent(
at java.awt.LightweightDispatcher.dispatchEvent(
at java.awt.Container.dispatchEventImpl(
at java.awt.Component.dispatchEvent(
at java.awt.EventQueue.dispatchEvent(
at java.awt.EventDispatchThread.pumpOneEvent(
at java.awt.EventDispatchThread.pumpEvents(
The following is the code i have used

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.border.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class MultiLineCellExample extends JApplet {
static int columnCount=4;
/* static int rowCount=3;*/
public void init() {
public void start() {
void main() {

DefaultTableModel dm = new DefaultTableModel() {
public Class getColumnClass(int columnIndex) {
return String.class;
dm.setDataVector(new Object[][]{
new Object[]{"1","2","3"});

JTable table = new JTable( dm );
MultiLineCellEditor editor = new MultiLineCellEditor(table);

TablePopupListener pop = new TablePopupListener(table);

table.setAutoCreateColumnsFromModel(false) ;

JScrollPane scroll = new JScrollPane( table );

scroll.setBorder(new javax.swing.border.TitledBorder("Table"));

JPanel des = new JPanel() ;
des.setLayout(new BorderLayout());

JPanel buttonPanel = new JPanel();

buttonPanel.setLayout(new GridLayout(7, 1));
buttonPanel.setBorder(new javax.swing.border.TitledBorder("Buttons"));

AddRowAction act = new AddRowAction(table);
InsertRowAction irt = new InsertRowAction(table);
InsertColAction ict = new InsertColAction(table);
AddColAction acoct = new AddColAction(table);
RemoveColAction coroct = new RemoveColAction(table);
RemoveRowAction roct = new RemoveRowAction(table);

buttonPanel.add(new JButton(act));
buttonPanel.add(new JButton(acoct));
buttonPanel.add(new JButton(ict));
buttonPanel.add(new JButton(irt));
buttonPanel.add(new JButton(coroct));
buttonPanel.add(new JButton(roct));

des.add(buttonPanel, BorderLayout.EAST);
des.add(scroll, BorderLayout.CENTER);

catch (Exception e)
System.out.println("ArrayIndexOutOfBoundsException in main");

The code used for removing the row is here

/* Remove rows */
class RemoveRowAction extends AbstractAction implements ActionListener, TableModelListener{
private JTable tab;
RemoveRowAction(JTable tabtest) { = tabtest;
putValue(Action.NAME, "Remove Selected Row");
public void actionPerformed(ActionEvent e) {
int row = tab.getSelectedRow() ;
int rowCount = tab.getRowCount();
//System.out.println("row = "+row);
if(row >=0 && row < rowCount)

System.out.println("row = "+row);
System.out.println("rowCount = "+rowCount);

Can any one help me how to handle this exception
[ November 01, 2002: Message edited by: khad M ]
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to post your code before anyone can diagnose this.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!