Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ArrayIndexOutOfBounds Exception in DefaultTableModel

421 views
Skip to first unread message

Rohan Chaudhari via JavaKB.com

unread,
Jan 4, 2005, 2:07:42 AM1/4/05
to
Hi,

I have created a Search Panel which displays the search result using JTable. If I select the first row in the JTable and then try to clear the results in table, it do not get any exception. But if I select a row other than the first row and then try to clear the table resuls, I get ArrayIndexOutOfBounds Exception.

Stack Trace Below:

java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Vector.java:412)
at com.dassault_systemes.vpm.Query.Model.VPMQueryModelBase.getXNodeAt(VP
MQueryModelBase.java:584)
at com.dassault_systemes.vpm.Search.command.BookmarkFromSearchCmdHeader.
valueChanged(BookmarkFromSearchCmdHeader.java:56)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSel
ectionModel.java:165)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSel
ectionModel.java:145)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSel
ectionModel.java:192)
at javax.swing.DefaultListSelectionModel.removeIndexInterval(DefaultList
SelectionModel.java:498)
at javax.swing.JTable.tableRowsDeleted(JTable.java:2802)
at javax.swing.JTable.tableChanged(JTable.java:2706)
at javax.swing.table.AbstractTableModel.fireTableChanged(AbstractTableModel.java:257)
at javax.swing.table.AbstractTableModel.fireTableRowsDeleted(AbstractTableModel.java:222)
at javax.swing.table.DefaultTableModel.removeRow DefaultTableModel.java:
585)
at com.dassault_systemes.vpm.Search.view.SearchResultPanel$ResultTableMo
del.removeAll(SearchResultPanel.java:1721)
at com.dassault_systemes.vpm.Search.view.SearchResultPanel.ClearRows(Sea
rchResultPanel.java:1100)
at com.dassault_systemes.vpm.Search.view.SearchResultView.display(Search
ResultView.java:66)
at com.dassault_systemes.vpm.Search.catlet.SearchResultCATlet.propertyCh
ange(SearchResultCATlet.java:525)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:147)
at com.dassault_systemes.vpm.Query.Model.VPMQueryModelBase.setDataVector
(VPMQueryModelBase.java:2003)
at com.dassault_systemes.vpm.Query.Model.VPMQueryModelBase.removeAllResu
lts(VPMQueryModelBase.java:2011)
at com.dassault_systemes.vpm.Search.catlet.SearchResultCATlet.clearAllRe
sults(SearchResultCATlet.java:855)
at com.dassault_systemes.vpm.Search.command.ClearResultCmd.start(ClearResultCmd.java:39)
at com.dassault_systemes.catweb.base.catlet.command.Command.doStart(Comm
and.java:189)
at com.dassault_systemes.catweb.base.catlet.CommandSelector.commandSelec
ted(CommandSelector.java:108)
at com.dassault_systemes.catweb.base.catlet.command.Command.fireCommandE
vent(Command.java:314)
at com.dassault_systemes.catweb.base.catlet.command.CmdHeader.startComma
nd(CmdHeader.java:1068)
at com.dassault_systemes.catweb.base.catlet.command.CmdHeader.actionPerf
ormed(CmdHeader.java:1041)
at com.dassault_systemes.catweb.base.catlet.CmdRep.fireActionEvent(CmdRe
p.java:73)
at com.dassault_systemes.catweb.base.catlet.CmdButtonRep.actionPerformed
(CmdButtonRep.java:87)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:14
45)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Abstra
ctButton.java:1499)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:373)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:245
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:211)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:2
25)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:2
25)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:2
25)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:2
25)
at java.awt.Component.processMouseEvent(Component.java:3710)
at java.awt.Component.processEvent(Component.java:3539)
at java.awt.Container.processEvent(Container.java:1159)
at java.awt.Component.dispatchEventImpl(Component.java:2588)
at java.awt.Container.dispatchEventImpl(Container.java:1208)
at java.awt.Component.dispatchEvent(Component.java:2492)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:2451)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2216)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2125)
at java.awt.Container.dispatchEventImpl(Container.java:1195)
at java.awt.Window.dispatchEventImpl(Window.java:923)
at java.awt.Component.dispatchEvent(Component.java:2492)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:334)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:126)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:88)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:80)


Code for clearing the table results:

public void removeAll()
{
int rows = getRowCount();
for (int i = 0; i < rows; i++)
removeRow(0);
}

I found that : if I select a row other than the first one and try to remove a row using removeRow(0), a valueChanged() event of DefaultListSelectionModel is getting fired. However, this event is not getting fired when I select the first row and then remove the row.

If anybody has any idea about this, kindly help me understand this behaviour.

Thanking you in anticipation

--
Message posted via http://www.javakb.com

Andrew McDonagh

unread,
Jan 4, 2005, 3:44:17 AM1/4/05
to
Rohan Chaudhari via JavaKB.com wrote:
> Hi,
>

snipped exception...

> Code for clearing the table results:
>
> public void removeAll()
> {
> int rows = getRowCount();
> for (int i = 0; i < rows; i++)
> removeRow(0);
> }

................../\

you are always removing row 0 - i think you meant to remove row(i);

Michael Sashnikov via JavaKB.com

unread,
Jan 4, 2005, 9:07:18 AM1/4/05
to
It happens because your Vector class reduces the number
of items every time you call the removeRow method.
If you want to use i then do something like this:

public void removeAll()
{
int rows = getRowCount();

for (int i = rows - 1; i >= 0; i--)
removeRow(i);

Rohan Chaudhari via JavaKB.com

unread,
Jan 5, 2005, 1:55:09 AM1/5/05
to
Hi,

I don,t think using i as parameter to removeRow(..) will work. The reason for this is whenever u call removeRow() a tableRowDeleted() event is fired and then a tableChanged() event . If I use i, then at one point of time, it will as its throw an exception becoz it will try to remove a row which has already been removed.

Thankx & Regards,
Rohan

Michael Sashnikov via JavaKB.com

unread,
Jan 5, 2005, 8:32:32 AM1/5/05
to
If you use for(int i = rows - 1; i >= 0; i--) {removeRow(i);}
then 'i' should work fine (unless you have a bug somewhere else).

The problem happens when you are using
for (int i = 0; i < rows; i++) {removeRow(i);}

In the second case every iteration increases 'i' value but reduces the total number of rows (the number of items in the Vector that stores the row objects). As a result at the middle of the loop it throws the ArrayIndexOutOfBoundsException exception.

Rohan Chaudhari via JavaKB.com

unread,
Jan 5, 2005, 11:30:30 PM1/5/05
to
Hi,

I tried the other option(of decrementing i) also. But then also, the ListSelection Event is fired and ultimately an exception is thrown.

Note: The same piece of code working fine in my application whose Build Time is jdk1.1.8 and Run Time is 1.3.1_08. But now,in my next release both my Run time and Build Time are 1.3.1_08. There it creates the problem.

Andrew McDonagh

unread,
Jan 6, 2005, 4:23:03 AM1/6/05
to
Rohan Chaudhari via JavaKB.com wrote:
> Hi,
>
> I have created a Search Panel which displays the search result using JTable. If I select the first row in the JTable and then try to clear the results in table, it do not get any exception. But if I select a row other than the first row and then try to clear the table resuls, I get ArrayIndexOutOfBounds Exception.
>
> Stack Trace Below:
>
> java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
> at java.util.Vector.elementAt(Vector.java:412)
> at com.dassault_systemes.vpm.Query.Model.VPMQueryModelBase.getXNodeAt(VP
> MQueryModelBase.java:584)
> at com.dassault_systemes.vpm.Search.command.BookmarkFromSearchCmdHeader.
> valueChanged(BookmarkFromSearchCmdHeader.java:56)
> at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSel
> ectionModel.java:165)
> at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSel
> ectionModel.java:145)
> at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSel
> ectionModel.java:192)
> at javax.swing.DefaultListSelectionModel.removeIndexInterval(DefaultList
> SelectionModel.java:498)
> at javax.swing.JTable.tableRowsDeleted(JTable.java:2802)
> at javax.swing.JTable.tableChanged(JTable.java:2706)
> at javax.swing.table.AbstractTableModel.fireTableChanged(AbstractTableModel.java:257)
> at javax.swing.table.AbstractTableModel.fireTableRowsDeleted(AbstractTableModel.java:222)
> at javax.swing.table.DefaultTableModel.removeRow DefaultTableModel.java:

snipped rest of exception code.

>
>
> Code for clearing the table results:
>
> public void removeAll()
> {
> int rows = getRowCount();
> for (int i = 0; i < rows; i++)
> removeRow(0);
> }
>
> I found that : if I select a row other than the first one and try to remove a row using removeRow(0), a valueChanged() event of DefaultListSelectionModel is getting fired. However, this event is not getting fired when I select the first row and then remove the row.
>
> If anybody has any idea about this, kindly help me understand this behaviour.
>
> Thanking you in anticipation
>

Ok, I've just re-read your post and realised that there is a solution to
your
problem and another 'problem' that you may not be aware of.

Namely, your removeAll() method is calling removeRow() upon the
DefaultTableModel .... in a LOOP.

So, first time in loop...

We call removeRow() method which then calls
AbstractTableModel.fireTableRowsDeleted(startingRow, finsihedRow),
which in turn informs the JTable to update its visuals. See the trimmed
stack trace above.

Then the next iteration of the loop starts, meaning that the
AbstractTableModel.fireTableRowsDeleted(...) method is call AGAIN, which
means the JTable updates its visuals AGAIN.

Then we do the next loop iteration....you can see where this is going...

The DefaultTableModel is calling the
AbstractTableModel.fireTableRowsDeleted(...) method with the same values
for firstRow and lastRow, when you call its removeRow() method.

However, the AbstractTableModel.fireTableRowsDeleted(...) method can
actually be called with any value. Therefore you could remove all data
from the Model, and call AbstractTableModel.fireTableRowsDeleted(...)
method once with the actual firstRow of 0 and the lastRow as
whatever number that is.

Not only does this save your problems with the for loop, it will
actually improve performance of the GUI seeing as posting of many events
is replaced with a single event.

The DefaultTableModel already has a method to do this -

public void setRowCount(int rowCount);

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/DefaultTableModel.html#setRowCount(int)

for info on the AbstractTableModel.fireTableRowsDeleted(...) have a look
at :

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/AbstractTableModel.html#fireTableRowsDeleted(int,%20int)

steve

unread,
Jan 6, 2005, 4:50:13 PM1/6/05
to
On Tue, 4 Jan 2005 22:07:18 +0800, Michael Sashnikov via JavaKB.com wrote
(in article <e81726ad09ef42c1...@JavaKB.com>):

> It happens because your Vector class reduces the number
> of items every time you call the removeRow method.
> If you want to use i then do something like this:
>
> public void removeAll()
> {
> int rows = getRowCount();
> for (int i = rows - 1; i >= 0; i--)
> removeRow(i);
> }
>
>

why the hell are you doing this?
when you can do something like this

Vector newmapping_list = new Vector();
....
...
...

newmapping_list.removeAllElements(); // empty the supplier list

then just map newmapping_list to your table model.

steve

0 new messages