Friday, October 9, 2015

Re: uncaught java.lang.indexoutofboundsexception



On Friday, October 9, 2015 at 12:32:59 PM UTC+2, NewbieGwtUser wrote:
I already expected my error and it worked fine by removing the = operator.

but unfortunately when I wanted to add the real code expected to be run within the for loop, I have had the same exception, and I also had only 1 displayed item in 2 (so that if I have 10 items, it display only 5 by skeeping every time 1 item), here is the code
              

Let's say you have 3 items.
 
              for(int i=0; i<nbMenuItems; i++){
MaterialToast.alert("ok"+ subMenuItemsContainer.getWidget(i));
MaterialRow row = new MaterialRow();
MaterialColumn col1=new MaterialColumn( 12, 6, 3);
col1.add(subMenuItemsContainer.getWidget(i));

You've just moved the first widget from subMenuItemsContainer to col1, so subMenuItemsContainer now only has 2 items.
The one at index 0 is now the one that previously was at index 1, and the one at index 1 is the one that was at index 2.

row.add(col1);
mosaiqueContainer.add(row);
}
}

I really don't understand what is the wrong with my code ???

On the second iteration of the loop, i will be 1, so subMenuItemsContainer will return the widget that initially was at index 2 (you just skipped the one that was at index 1, which has moved to index 0).
And during that iteration, you removed that item too, so subMenuItemsContainer now only contains 1 item (the one that initially was at index 1).
On the third iteration, i is 2, but SubMenuItemsContainer only has 1 item ⇒ exception.

Solution ⇒ replace your "for (int i = 0; i < nbMenuItems; i++)" with "while (subMenuItemsContainer.getWidgetCount() > 0)", and always use getWidget(0).
…except I suspect you'd actually rather not to move the widgets around, but rather somehow copy them or whatever.

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment