Search code examples
javagwtlistboxgwt2uibinder

Listbox outofbound error


I have a trying to put value from arrayList to List box. but it gives me error out of bound exception.

getListValue() returns arrayList.

for(int i = 0;i<getListValue().size();i++){
                System.out.println("qsdq " +getListValue().toString()+" "+ getListValue().size());
                listBox.addItem(getListValue().get(i)); // ErrorPoint
            }

output qsdq [xyz, abc] 2

Stacktrace

    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.IndexOutOfBoundsException: null
    at com.google.gwt.user.client.ui.ListBox.checkIndex(ListBox.java:595)
    at com.google.gwt.user.client.ui.ListBox.setValue(ListBox.java:511)
    at com.client.GUI.MultivaluedPopup.getListBox(MultivaluedPopup.java:92) // error point

Solution

  • You should probably add the items first. The setValues only sets the value (as the name suggests) but no new item is added.

    Use listBox.Items.add before setting the value, or add the new item with right value in the first place by add.

    ArrayList list = getListValue();
    listBoc.Items.clear();
    for(int i = 0;i<list.size();i++){
                    System.out.println("qsdq " +list.toString()+" "+ list.size());
                    listBox.addItem(list.get(i));
                }