• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Throwing an Exception when deleting a row

 
VenuArchana Aradhyula
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I want to delete the selected row from the table.The table consists of 3 columns Select,Option , Option Data. It is deleting the row but it is also throwing array index out of Bounds Exception.

The code is

[ October 13, 2008: Message edited by: VenuArchana Aradhyula ]
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is that you are running the delete code in a loop.
What do you think getRowCount will return when your call to addOptDefTblModel.removeRow(i); is successful?
 
VenuArchana Aradhyula
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is to remove multi rows at a time.
when the check box in the Select column is checked.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by VenuArchana Aradhyula:
This is to remove multi rows at a time.
when the check box in the Select column is checked.


Yup
I could figure that out from your code

When your call executes, the row index is decremented by one and that is the reason you are getting the exception.
 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you need to delete something from any object (JTable, JList, java.util.List) based on the index, I find it easier to start at the end:

The problem with going forward regularly is, like Maneesh said, the contents get shifted back one index if you remove.

Suppose you have a list with 5 elements, from 0 to 4. The indexing is as follows: 0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4

If you now remove the third element, the indexing is changed: 0 => 0, 1 => 1, 2 => 3, 3 => 4. The size also changes.

With your type of code you would get the following:
- check 0; value 0 is skipped
- check 1; value 1 is skipped
- check 2; value 2 is removed
- check 3; value 4 (not 3!) is skipped
- check 4; doesn't exist!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic