0

java.lang.IndexOutOfBoundsException in listbox

asked 2016-03-08 22:48:17 +0800

arcangelSalazar gravatar image arcangelSalazar
36 2

I have this ViewModel:

package mx.mindwork.relativity6.controllers.clients;

import java.util.ArrayList; import java.util.List;

import org.zkoss.bind.annotation.Command; import org.zkoss.bind.annotation.Init; import org.zkoss.zk.ui.Executions;

import mx.mindwork.relativity6.models.clients.Client;

public class ClientListVM {

private List<Client> clients;
private int selectedIndex;

@Init
public void init() {
    clients = new ArrayList<>();
}

@Command
public void newClient(){
    Executions.sendRedirect("/clients/clientsForm.zul");
}

public List<Client> getClients() {
    return clients;
}

public int getSelectedIndex() {
    return selectedIndex;
}

public void setSelectedIndex(int selectedIndex) {
    this.selectedIndex = selectedIndex;
}

}

And this view:

<zk xmlns:n="native">

<n:h2>Clients List</n:h2> <listbox model="@load(vm.clients)" selectedindex="@bind(vm.selectedIndex)" emptymessage="There are no clients" height="300px"> <listhead> <listheader label="ID"/> <listheader label="ID Ext"/> <listheader label="First Name"/> <listheader label="Last Name"/> <listheader label="Email"/> <listheader label="Phone"/> <listheader label="Company"/> </listhead> <template name="model" var="client"> <listitem> <listcell label="@load(client.id)"/> <listcell label="@load(client.idExt)"/> <listcell label="@load(client.firstName)"/> <listcell label="@load(client.lastName)"/> <listcell label="@load(client.email)"/> <listcell label="@load(client.phone)"/> <listcell label="@load(client.company.name)"/> </listitem> </template> </listbox> <button label="New Client" onclick="@command('newClient')"/>
</zk>

If I use as is, I get this error:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 java.util.ArrayList.rangeCheck(ArrayList.java:653) java.util.ArrayList.get(ArrayList.java:429) org.zkoss.zul.ListModelList.getElementAt(ListModelList.java:156) org.zkoss.bind.converter.sys.AbstractSelectedIndexConverter.coerceToUi(AbstractSelectedIndexConverter.java:50) org.zkoss.bind.impl.LoadPropertyBindingImpl.load(LoadPropertyBindingImpl.java:74) org.zkoss.bind.impl.PropertyBindingHandler.doLoadBinding(PropertyBindingHandler.java:133) org.zkoss.bind.impl.PropertyBindingHandler.doLoad(PropertyBindingHandler.java:334) org.zkoss.bind.impl.BinderImpl.loadComponentProperties0(BinderImpl.java:2307) org.zkoss.bind.impl.BinderImpl.loadComponent0(BinderImpl.java:2276) org.zkoss.bind.impl.BinderImpl.loadComponent0(BinderImpl.java:2281) org.zkoss.bind.impl.BinderImpl.loadComponent(BinderImpl.java:2215) org.zkoss.bind.BindComposer$BinderKeeper$Loader.load(BindComposer.java:575) org.zkoss.bind.BindComposer$BinderKeeper.loadComponentForAllBinders(BindComposer.java:551) org.zkoss.bind.BindComposer.doAfterCompose(BindComposer.java:190) org.zkoss.zk.ui.impl.UiEngineImpl.doAfterCompose(UiEngineImpl.java:588) org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild0(UiEngineImpl.java:931) org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:877) org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:748) org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:822) org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:771) org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:709) org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:452) org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:358) org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:217) org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:136) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

If I create some dummy data and add to the clients array, works as expected. Any hints to make this work with empty array?

delete flag offensive retag edit

Comments

Hello! Which ZK version are you using? i tested your code with ZK8 and i cannot reproduce - it works like a charm even when the list is empty.

gganassin ( 2016-03-09 14:50:18 +0800 )edit

I'm using ZK 8.0.0.

arcangelSalazar ( 2016-03-10 21:23:55 +0800 )edit

No, the same error with ZK 8.0.1.1 CE (build: 2016020312)

arcangelSalazar ( 2016-03-10 22:50:38 +0800 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-03-10 23:07:16 +0800

arcangelSalazar gravatar image arcangelSalazar
36 2

After some debuggin I found the cause of the problem. selectedIndex by default is 0 and is causing all the troubles because the models wants to put 0 as selected in empty model. If you add selectedindex = -1; in the init the thing works as expected.

link publish delete flag offensive edit

Comments

isn't it easier to use selectedItem in stead of selectedIndex? selectedItem is in your VM a Client and you just need to provide a getter and setter for it. null value => same as -1.

chillworld ( 2016-03-11 07:18:31 +0800 )edit

Yes, that is the new approach I am using.

arcangelSalazar ( 2016-03-13 03:15:20 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2016-03-08 22:48:17 +0800

Seen: 23 times

Last updated: Mar 10 '16

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More