Out of bound exception in addTab

Hello all,

When adding a tab to a TabSheet I get an out of bounds exception that I cannot understand.

Exception in thread “Thread-1286” java.lang.IndexOutOfBoundsException: Index: 29, Size: 28
at java.util.ArrayList.rangeCheckForAdd(ArrayList.java:661)
at java.util.ArrayList.add(ArrayList.java:473)
at com.vaadin.ui.TabSheet.addTab(TabSheet.java:390)
at com.vaadin.ui.TabSheet.addTab(TabSheet.java:355)
at com.vaadin.ui.TabSheet.addTab(TabSheet.java:331)

Can anyone tell me what is going wrong?

Thanks in advance.

I am using Vaading 7.6.6.

Albert

Hi,

if I’m looking at the right place in code, it would be coming from the four-parameter addTab method, which takes the position integer. It looks like this:

[code]
public Tab addTab(Component tabComponent, String caption, Resource icon,
int position) {
if (tabComponent == null) {
return null;
} else if (tabs.containsKey(tabComponent)) {
Tab tab = tabs.get(tabComponent);
tab.setCaption(caption);
tab.setIcon(icon);
return tab;
} else {
components.add(position, tabComponent);

        TabSheetTabImpl tab = new TabSheetTabImpl(
                keyMapper.key(tabComponent), caption, icon);

        getState().tabs.add(position, tab.getTabState());
        tabs.put(tabComponent, tab);

        if (selected == null) {
            setSelected(tabComponent);
            fireSelectedTabChange();
        }

        super.addComponent(tabComponent);

        return tab;
    }
}

[/code]The error in question is coming, I think, from the components.add(position, tabComponent) line, and components member is an ArrayList. If position is larger than the size of components, it throws that kind of exception you’re getting. Looks like you shouldn’t use that particular overload of addTab if you want to add a tab to a position that’s not yet there. To work around it, if you want to add an icon, you can do that to an existing tab (so you’ll end up in the else if branch).

Best regards,

Olli

Hello Olli,

Thanks for your answer.
After having looking into the code, I came to the conclusions it must be a concurrency problem.
I actually found the place where the tab was added, and it was not synchronised properly.
This must have been the problem.

Albert

Good to hear you found out the cause :slight_smile:

If you have experienced an issue like this, I strongly urge you to review your code for other places where such concurrent access may happen. Generally, Vaadin components are not thread-safe, exactly in a way HashMap isn’t thread safe:

  1. Vaadin components must not be accessed from any other thread than the UI thread
  2. Yet if they are, you won’t get an exception; instead the internal component state may get corrupted which may result in random and hard-to-find bugs.

A golden rule is to access Vaadin components from the UI thread only - always use ui.access(Runnable) to mutate Vaadin components from other threads. If you don’t, you will experience such random issues also in the future.