org.zkoss.zk.ui.UiException and java.lang.IndexOutOfBoundsException in java?

2.1k views Asked by At

I have an issue regarding UiException and IndexOutOfBoundsException. Here is my stack trace:

Logcatlog:

Dec 10, 2013 11:03:49 AM org.zkoss.zk.ui.impl.UiEngineImpl handleError:1359
SEVERE: >>org.zkoss.zk.ui.UiException: Index: 1, Size: 0
>>java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
>>  at java.util.ArrayList.rangeCheckForAdd(Unknown Source)
>>  at java.util.ArrayList.add(Unknown Source)
>>  at com.csdcsystems.amanda.web.viewmodel.ToolbarCustomizeViewModel.getModulesList(ToolbarCustomizeViewModel.java:187)
>>  at com.csdcsystems.amanda.web.viewmodel.ToolbarCustomizeViewModel.afterCompose(ToolbarCustomizeViewModel.java:93)
>>  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>>  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>>  at java.lang.reflect.Method.invoke(Unknown Source)
>>  at org.zkoss.bind.impl.ParamCall.call(ParamCall.java:111)

zul:

<zk xmlns:x="xhtml" xmlns:zk="zk">
    <window apply="org.zkoss.bind.BindComposer"
            viewModel="@id('vm') @init('com.csdcsystems.amanda.web.viewmodel.ToolbarCustomizeViewModel')" title="Customize Toolbar" height="600px"
        width="600px" closable="true" focus="true">
        <borderlayout width="100%" height="100%" >
            <north border="none">
                <hlayout>
                    <space width="5px" />
                    <x:table border="0">
                        <x:tr>
                            <x:td>
                                <label
                                    value="Display label along with icons" sclass="group-header" />
                            </x:td>
                            <radiogroup
                                selectedItem="@bind(vm.displayIcon)">
                                <x:td valign="center">
                                    <radio label="Display" 
                                        value="display" focus="@load(vm.displayFocus)"/>
                                </x:td>
                                <x:td valign="center">
                                    <radio label="Hide" value="hide" focus="@load(vm.hideFocus)"/>
                                </x:td>
                            </radiogroup>
                        </x:tr>

                    </x:table>
                </hlayout>
            </north>
            <center border="none">
                <hlayout style="padding: 10px;" hflex="1" vflex="1">
                    <separator />
                    <listbox model="@bind(vm.haveList)" droppable="true"
                        onDrop="@command('dropToHaveList',item=event.dragged.attributes.item)"
                        hflex="1" vflex="1">
                        <listhead>
                            <listheader
                                label="${a:resource('ValidUserSetup:LABEL_HAVE')}" />
                        </listhead>
                        <template name="model" var="tbaritem">
                            <listitem draggable="true" droppable="true"
                                label="@load(tbaritem.name)" attributes.item="@load(tbaritem)"
                                onDrop="@command('insertToHaveList',item=event.dragged.attributes.item, base=tbaritem)">
                                <listcell
                                    image="@load(tbaritem.imagePath)" />
                            </listitem>
                        </template>
                    </listbox>
                    <separator />
                    <listbox model="@bind(vm.availableList)"
                        droppable="true"
                        onDrop="@command('dropToAvailableList',item=event.dragged.attributes.item)"
                        hflex="1" vflex="1" multiple="true">
                        <listhead>
                            <listheader
                                label="${a:resource('ValidUserSetup:LABEL_AVAILABLE')}" />
                        </listhead>
                        <template name="model" var="tbaritem">
                            <listitem draggable="true" droppable="true"
                                label="@load(tbaritem.name)" attributes.item="@load(tbaritem)"
                                onDrop="@command('insertToAvailableList',item=event.dragged.attributes.item, base=tbaritem)">
                                <listcell
                                    image="@load(tbaritem.imagePath)" />
                            </listitem>
                        </template>
                    </listbox>
                    <separator />
                </hlayout>
            </center>
            <south border="none">
                <hlayout style="padding: 10px;">
                    <space width="35px" />
                    <button
                        label="${a:resource('ValidUserSetup:CB_HAVEALL')}"
                        onClick="@command('haveAll')" />
                    <space width="15px" />
                    <button
                        label="${a:resource('ValidUserSetup:CB_HAVENONE')}"
                        onClick="@command('haveNone')" />

                    <space width="125px" />
                    <button onClick="@command('save')" label="Save"/>
                    <space width="15px" />
                    <button onClick="@command('cancel')" label="Cancel" />
                </hlayout>
            </south>
        </borderlayout>
    </window>
</zk>

Here's Java code :

int toolsCount =AbcdConstants.toolbar_image.length;
                int maxtool = AbcdConstants.S_TOOLBAR.length();
                LOGGER.debug("toolbar not set for user group");
                for (int index = 0, tbarIndex = 1; index < toolsCount; index++) {
                    if (AbcdConstants.toolbar_name[1][index].equalsIgnoreCase("true") && tbarIndex < maxtool) {
                        if (AbcdResourceBundle.get("TOOLBAR_EXECUTIVE_MONITOR").equals(
                                AbcdResourceBundle.get(AbcdConstants.toolbar_name[0][index]))
                                && (module.charAt(28) == 'N' || !AbcdLicence.get().isExecutiveMonitorEnabled())) {
                            continue;
                        }
                        imageName.add(AbcdConstants.toolbar_image[index]);
                        iconDisplayName.add(AmandaResourceBundle.get(AbcdConstants.toolbar_name[0][index]));
                        toolbarIndexValue.add(tbarIndex, String.valueOf(tbarIndex + 2));
                        ToolbarItem thisitem = new ToolbarItem();
                        thisitem.setItemIndex((tbarIndex + 2));
                        thisitem.setName(AbcdResourceBundle.get(AbcdConstants.toolbar_name[0][index]));
                        thisitem.setImagePath(IMAGE_PATH + AbcdConstants.toolbar_image[index].toLowerCase());
                        allItems.add(thisitem);
                    }
                    tbarIndex += 2;
                }

The exception occurs in this line :

toolbarIndexValue.add(tbarIndex, String.valueOf(tbarIndex + 2));

Can anyone tell me how can I resolve this issue ?

Thanks

2

There are 2 answers

0
Hilikus On BEST ANSWER

You are trying to add an element in a position that doesn't exist. Try to preallocate the space when you construct the ArrayList by using the int constructor

toolbarIndexValue = new ArrayList(256); //or some decent estimate

Alternatively, allocate it lazily using ensureCapacity() if you don't have a decent estimate at construction time
Finally, if you don't mind the order, just call add(String.valueOf(tbarIndex + 2))

0
Sitansu On

It works remove the index because arraylist by default create index:

 toolbarIndexValue.add( String.valueOf(tbarIndex + 2));