Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » arrayindexoutofbounds when adding a new element to a tableviewer
arrayindexoutofbounds when adding a new element to a tableviewer [message #517102] Fri, 26 February 2010 09:30 Go to next message
Luca Ferrari is currently offline Luca FerrariFriend
Messages: 159
Registered: November 2009
Senior Member
Hi,
I've got a strange behaviour of a table viewer: I add to remove all elements displayed and substitute them with a new array of objects, so I wrote the following code:

this.tableViewer.getTable().removeAll();
	    
this.tableViewer.add( event.getDataCollectionAsArray() );	


the getDataCollectionAsArray() method returns an array of objects (filled) with data to be shown in the table. However, when I run this code I got the following exception:


!ENTRY org.eclipse.jface 4 2 2010-02-26 10:28:42.989
!MESSAGE Problems occurred when invoking code from plug-in: "org.eclipse.jface".
!STACK 0
java.lang.ArrayIndexOutOfBoundsException
	at java.lang.System.arraycopy(Native Method)
	at org.eclipse.jface.viewers.AbstractTableViewer$VirtualManager.notVisibleAdded(AbstractTableViewer.java:127)
	at org.eclipse.jface.viewers.AbstractTableViewer.createItem(AbstractTableViewer.java:280)
	at org.eclipse.jface.viewers.AbstractTableViewer.add(AbstractTableViewer.java:263)
	at hrpm.rcp.gui.view.BaseTableView.dataModelChangeEvent(BaseTableView.java:954)


line 954 is the one when I call the add method of the tableviewer. Any idea about what can cause this problem? Digging the code I've found that the exception is thrown when there is the array copy of cached elements into the table, but I don't understand why the index is going out of bounds.
Re: arrayindexoutofbounds when adding a new element to a tableviewer [message #517113 is a reply to message #517102] Fri, 26 February 2010 10:15 Go to previous messageGo to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
Why to use a tableviewer and do operations on table...

If you want to update your tableviewer you do a...

tableviewer.setInput()...

you dont even need to do a remove operation...


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay

[Updated on: Fri, 26 February 2010 10:17]

Report message to a moderator

Re: arrayindexoutofbounds when adding a new element to a tableviewer [message #517117 is a reply to message #517102] Fri, 26 February 2010 10:22 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Vijay is right but the real problem is that I think add/removing items
in a virtual tableviewer is not really working appropriately.

Tom

Am 26.02.10 10:30, schrieb Luca Ferrari:
> Hi,
> I've got a strange behaviour of a table viewer: I add to remove all
> elements displayed and substitute them with a new array of objects, so I
> wrote the following code:
>
>
> this.tableViewer.getTable().removeAll();
> this.tableViewer.add( event.getDataCollectionAsArray() );
>
>
> the getDataCollectionAsArray() method returns an array of objects
> (filled) with data to be shown in the table. However, when I run this
> code I got the following exception:
>
>
>
> !ENTRY org.eclipse.jface 4 2 2010-02-26 10:28:42.989
> !MESSAGE Problems occurred when invoking code from plug-in:
> "org.eclipse.jface".
> !STACK 0
> java.lang.ArrayIndexOutOfBoundsException
> at java.lang.System.arraycopy(Native Method)
> at
> org.eclipse.jface.viewers.AbstractTableViewer$VirtualManager .notVisibleAdded(AbstractTableViewer.java:127)
>
> at
> org.eclipse.jface.viewers.AbstractTableViewer.createItem(Abs tractTableViewer.java:280)
>
> at
> org.eclipse.jface.viewers.AbstractTableViewer.add(AbstractTa bleViewer.java:263)
>
> at
> hrpm.rcp.gui.view.BaseTableView.dataModelChangeEvent(BaseTab leView.java:954)
>
>
>
> line 954 is the one when I call the add method of the tableviewer. Any
> idea about what can cause this problem? Digging the code I've found that
> the exception is thrown when there is the array copy of cached elements
> into the table, but I don't understand why the index is going out of
> bounds.
Re: arrayindexoutofbounds when adding a new element to a tableviewer [message #517130 is a reply to message #517117] Fri, 26 February 2010 11:14 Go to previous messageGo to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
In
org.eclipse.jface.viewers.AbstractTableViewer$VirtualManager .notVisibleAdded(AbstractTableViewer.java:127)

/**
		 * A non visible item has been added.
		 *
		 * @param element
		 * @param index
		 */
		public void notVisibleAdded(Object element, int index) {

			int requiredCount = doGetItemCount() + 1;

			Object[] newCache = new Object[requiredCount];
			System.arraycopy(cachedElements, 0, newCache, 0, index);
			if (index < cachedElements.length) {
				System.arraycopy(cachedElements, index, newCache, index + 1,
						cachedElements.length - index);
			}
			newCache[index] = element;
			cachedElements = newCache;

			doSetItemCount(requiredCount);
		}


array copy happens from src "cachedElements" to new array...

This cachedElements is updated only in refresh of the tableviewer...

Since Luca did table.removeAll hence the cachedElements was not updated hence the target array and source array to be copyed are of diffrent size...

Hence ArrayIndexOutOfBoundsException....


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: arrayindexoutofbounds when adding a new element to a tableviewer [message #517555 is a reply to message #517113] Mon, 01 March 2010 09:55 Go to previous messageGo to next message
Luca Ferrari is currently offline Luca FerrariFriend
Messages: 159
Registered: November 2009
Senior Member
vijay wrote on Fri, 26 February 2010 05:15
Why to use a tableviewer and do operations on table...

If you want to update your tableviewer you do a...

tableviewer.setInput()...

you dont even need to do a remove operation...


Thanks, it was really simple: setting the new input works great.
Re: arrayindexoutofbounds when adding a new element to a tableviewer [message #517556 is a reply to message #517130] Mon, 01 March 2010 09:55 Go to previous message
Luca Ferrari is currently offline Luca FerrariFriend
Messages: 159
Registered: November 2009
Senior Member
vijay wrote on Fri, 26 February 2010 06:14
In
org.eclipse.jface.viewers.AbstractTableViewer$VirtualManager .notVisibleAdded(AbstractTableViewer.java:127)

/**
		 * A non visible item has been added.
		 *
		 * @param element
		 * @param index
		 */
		public void notVisibleAdded(Object element, int index) {

			int requiredCount = doGetItemCount() + 1;

			Object[] newCache = new Object[requiredCount];
			System.arraycopy(cachedElements, 0, newCache, 0, index);
			if (index < cachedElements.length) {
				System.arraycopy(cachedElements, index, newCache, index + 1,
						cachedElements.length - index);
			}
			newCache[index] = element;
			cachedElements = newCache;

			doSetItemCount(requiredCount);
		}


array copy happens from src "cachedElements" to new array...

This cachedElements is updated only in refresh of the tableviewer...

Since Luca did table.removeAll hence the cachedElements was not updated hence the target array and source array to be copyed are of diffrent size...

Hence ArrayIndexOutOfBoundsException....



Thanks, now it is clear why I got the array index out of bound. Thanks for the explaination, really interesting.
Previous Topic:Popup menu from menu item, similar to IE Favorites popup
Next Topic:Determining if scrollbars are visible
Goto Forum:
  


Current Time: Sat Dec 07 22:20:28 GMT 2024

Powered by FUDForum. Page generated in 0.03470 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top