-1

我有一个关于 UiException 和 IndexOutOfBoundsException 的问题。这是我的堆栈跟踪:

日志猫日志:

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)

祖尔:

<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>

这是Java代码:

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;
                }

异常发生在这一行:

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

谁能告诉我如何解决这个问题?

谢谢

4

2 回答 2

2

您正在尝试在不存在的位置添加元素。尝试ArrayList使用int构造函数构造时预分配空间

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

或者,如果您在施工时没有合适的估计,请使用ensureCapacity()懒惰地分配它
最后,如果您不介意订单,只需致电add(String.valueOf(tbarIndex + 2))

于 2013-12-10T05:52:47.057 回答
0

它可以删除索引,因为 arraylist 默认创建索引:

 toolbarIndexValue.add( String.valueOf(tbarIndex + 2));
于 2013-12-20T11:40:58.690 回答