索引越界错误 [英] Index out of bounds error
问题描述
我正在开发一个程序,我正在将保存的小部件重新创建回边界面板.当我创建它们时,我也试图将值放入 ArrayList 中,这样如果我想更新和保存打开的项目,我应该能够通过从 ArrayList 获取值来做到这一点.
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.
代码如下:
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());
}
}
这里的ename、ername、etotalpartial、wename和wrname都是ArrayList.这段代码包含在一个异步类方法中.
Here ename, ername, etotalpartial, wename and wrname are all ArrayList. This piece of code is included in an asynchronous class method.
当我运行代码时,我在ername.add(ename......"处出现错误.
When i run the code i get error at "ername.add(ename......".
这是错误堆栈:
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.
谢谢.
推荐答案
看起来 ername 或 wrname 是一个空数组列表.ername.size() 可能会返回 0 顺便说一句...
Well it looks like ername or wrname is an empty array list. ername.size() will probably return 0 by the way...
您要么想使用 add(Object object) 方法,要么您可以通过插入以下代码来初始化容量:
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...
这篇关于索引越界错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!