This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Throwing an Exception when deleting a row Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Throwing an Exception when deleting a row" Watch "Throwing an Exception when deleting a row" New topic
Author

Throwing an Exception when deleting a row

VenuArchana Aradhyula
Greenhorn

Joined: Sep 02, 2008
Posts: 13
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

Joined: Jul 26, 2007
Posts: 10167
    
    8

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?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
VenuArchana Aradhyula
Greenhorn

Joined: Sep 02, 2008
Posts: 13
This is to remove multi rows at a time.
when the check box in the Select column is checked.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

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

Joined: Oct 27, 2005
Posts: 19649
    
  18

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!


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Throwing an Exception when deleting a row
 
Similar Threads
Calculate sum in jtable and display the sum in jtextfield
Exceptions thrown by respective Integer and Float functions differ....
ArrayIndexOutOfBounds Exception
how to delete multiple browse
creating a table