java.lang.indexoutofboundsexception 索引 0 大小 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21570417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:25:34  来源:igfitidea点击:

java.lang.indexoutofboundsexception index 0 size 0

javaindexoutofboundsexception

提问by rez

I've been getting this java.lang.indexoutofboundsexception error when adding system functions objects in an array through checkbox:

通过复选框在数组中添加系统函数对象时,我遇到了这个 java.lang.indexoutofboundsexception 错误:

SEVERE: Servlet.service() for servlet spis threw exception
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at com.spis.dao.impl.CodesValueDaoImpl.getDescription(CodesValueDaoImpl.java:95)
at com.spis.bo.impl.CodesValueBoImpl.getDescription(CodesValueBoImpl.java:103)
at com.spis.controller.SystemUserController.viewPaged(SystemUserController.java:408)
at com.spis.controller.SystemUserController.systemUser(SystemUserController.java:48)

Here is the for the systemFunctions: whenever i add systemFunction to a user it displays the error or when the systemFunction colummn in the database is null. But when i set systemFunction=" " in the database the page displays correctly although withouth the array of systemFunctions obviously..

以下是 systemFunctions:每当我向用户添加 systemFunction 时,它都会显示错误,或者当数据库中的 systemFunction 列为空时。但是当我在数据库中设置 systemFunction="" 时,页面显示正确,尽管显然没有 systemFunctions 数组..

String[] systemFunctions=(String[])request.getParameterValues("systemFunctions");
String appendedSysFunctions ="";

//Append system functions
if(systemFunctions!=null){
    for(int i=0;i<systemFunctions.length;i++){
        if(i==(systemFunctions.length-1)){
        appendedSysFunctions+=systemFunctions[i];
        }
        else{
        appendedSysFunctions+=systemFunctions[i] + ",";
        }
    }
}

Another code:

另一个代码:

StringTokenizer st = new StringTokenizer(su.getAllowedSysFunc(), ",");
while (st.hasMoreTokens()){
    appendedSystemFunc = appendedSystemFunc + codesValueMethods.getDescription 
    (3, st.nextToken());
    if(st.hasMoreTokens()){
    appendedSystemFunc = appendedSystemFunc + ", \n";
    }
}

回答by Omoro

You have to check systemFunctionswhether it is null and whether its length is greater than 0:

您必须检查systemFunctions它是否为空以及其长度是否大于 0:

if(systemFunctions!=null && systemFunctions.length>0){
  ...
}