0

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
GameBuilder
  • 1,169
  • 4
  • 31
  • 62

1 Answers1

1

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));
            }
Matzi
  • 13,770
  • 4
  • 33
  • 50
  • `listBox.addItem(getListValue().get(i));` coz of this the for loop is going in infinite loop.. – GameBuilder May 15 '12 at 11:45
  • Do you have the listbox populated? And the `geListValue()` gets the values from the listbox? – Matzi May 15 '12 at 11:49
  • `geListValue()` returns the array List. No due to this function only it will have some values. – GameBuilder May 15 '12 at 11:51
  • If the getListValue is not tied to the listbox, it yhould not run into an infinite loop. Try to store the array in a variable, and clear the listbox. See updated answer. – Matzi May 15 '12 at 11:53
  • Ya after taking `getListValue();` in varible it started working. but i didnot understand y this happened as both `getListValue() and list is arrayList `.. Can you explain. – GameBuilder May 15 '12 at 12:06
  • I suspect that the getListValue is somehow based on the listBox's values. So when you add another element into the listbox, it appears in the arraylist of the getListValue. So basically you keep adding elements infinitely to the list. But that is jsut a guess, I may need to know the code of the getListValue. – Matzi May 15 '12 at 12:11