views:

400

answers:

2

Hello,

I am working on a program where i am recreating the saved widgets back on to the boundary panel. When i am creating them i am also trying to put the values into the ArrayList so that if i want to update and save the opened project i should be able to do so by getting the values from the ArrayList.

Here is how the code looks like:

for(int i = 0; i < result.length; i++){
                if(ename.contains(result[i].getParticipateEntityName())){
                        ername.add(ename.indexOf(result[i].getParticipateEntityName()), result[i].getParticipateRelatioshipName());
                        etotalpartial.add(ename.indexOf(result[i].getParticipateEntityName()), result[i].getTotalPartial());
                }else if(wename.contains(result[i].getParticipateEntityName())){
                        wrname.add(wename.indexOf(result[i].getParticipateEntityName()), result[i].getParticipateRelatioshipName());
                }
}

Here ename, ername, etotalpartial, wename and wrname are all ArrayList. This piece of code is included in an asynchronous class method.

When i run the code i get error at "ername.add(ename......".

Here is the error stack:

java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    at java.util.ArrayList.add(ArrayList.java:367)
    at com.e.r.d.client.ERD1$16.onSuccess(ERD1.java:898)
    at com.e.r.d.client.ERD1$16.onSuccess(ERD1.java:1)
    at com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.onResponseReceived(RequestCallbackAdapter.java:216)
    at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:287)
    at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:393)
    at sun.reflect.GeneratedMethodAccessor16.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:157)
    at com.google.gwt.dev.shell.BrowserChannel.reactToMessagesWhileWaitingForReturn(BrowserChannel.java:1713)
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:165)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:120)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:507)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:264)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:188)
    at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:157)
    at com.google.gwt.dev.shell.BrowserChannel.reactToMessages(BrowserChannel.java:1668)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:401)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:222)
    at java.lang.Thread.run(Thread.java:619)

I am not sure what i am doing wrong.

Any input will be of great help.

Thank you.

+2  A: 

You can't use the public void add(int index, E element) method of List if you can not guarantee that the size of your list is greater than index.

That is what happening with your code. You have an empty list (ername), and ename.indexOf(result[i].getParticipateEntityName()) is returning 1, thus the IndexOutOfBoundsException. In other words, you have a list of size 0, and you are trying to insert an element at index 1.

Consider using the public boolean add(E e) method of ArrayList which appends the new element to the end of the list.

JG
is there way around this because i want the ename value and the ername value to correspond with each other in terms of index?Thank you.
sprasad12
You can use a `Map` instead, with the key being the `index`, and the value being `ename`.
JG
Or you can use an array initialized to the needed capacity.
Bluu
@Bluu: You'd be just wasting space.
JG
A: 

Well it looks like ername or wrname is an empty array list. ername.size() will probably return 0 by the way...

You either want to use the add(Object object) method, or you can initialize a capacity by inserting the following code:

for (int i = 0; i < CAPACITY; i++){
   ername.add(null);
}

and then your code should work...

m_oLogin