简体   繁体   English

如何解决数组索引 OutOfBoundsException

[英]How to resolve Array Index OutOfBoundsException

I have the below code.我有以下代码。 When I execute it, I get the below error.当我执行它时,我收到以下错误。 I run the same code in another step, it works fine, but in the current step it fails.我在另一个步骤中运行相同的代码,它工作正常,但在当前步骤中它失败了。

java.util.concurrent.ExecutionException: java.lang.ArrayIndexOutOfBoundsException: 17

++++++++++++++++++++++++++++++++++++++++++++++++++++++++

def dbData = queryResults;


def mailTable = "<table style='border: 1px solid #ccc;border-collapse: collapse;'>";
def size = 17;

mailTable += "<tr style='border-top: 3px solid #ccc;'><b><u><td style='padding: 10px;bgcolor=#4169E1;'>SKU</td><td style='padding: 10px;bgcolor=#4169E1;'>Currency</td><td style='padding:10px;bgcolor=#4169E1;'>Source</td><td style='padding: 10px;bgcolor=#4169E1;'>Pricing Sort</td><td style='padding: 10px;bgcolor=#4169E1;'>Role</td><td style='padding: 10px;bgcolor=#4169E1;'>Product Manager</td><td style='padding: 10px;bgcolor=#4169E1;'>Price</td><td style='padding: 10px;bgcolor=#4169E1;'>Layout</td><td style='padding: 10px;bgcolor=#4169E1;'>Description</td><td style='padding: 10px;bgcolor=#4169E1;'>Reference</td><td style='padding: 10px;bgcolor=#4169E1;'>Global</td><td style='padding: 10px;bgcolor=#4169E1;'>Status</td><td style='padding: 10px;bgcolor=#4169E1;'>Material</td><td style='padding: 10px;bgcolor=#4169E1;'>Business</td><td style='padding: 10px;bgcolor=#4169E1;'>SB</td><td style='padding: 10px;bgcolor=#4169E1;'>Group</td><td style='padding: 10px;bgcolor=#4169E1;'>GPH Code</td></u></b></tr>"


/**
 * filling the html table with opportunity info
 * */
def index;
dbData.each {row ->

    mailTable += "<tr style='border-top: 3px solid #ccc;'>";

    for (index = 0; index < size; ++index) {
        mailTable += "<td style='padding: 10px;'>" + row.getAt(index) + "</td>";
    }

    mailTable += "</tr>";

}
mailTable += "</table>";

You're getting the Array Index OutOfBoundsException because you're trying to access element in an empty Array (0 elements) most likely.您收到 Array Index OutOfBoundsException 是因为您最有可能尝试访问空数组(0 个元素)中的元素。 I'd suggest you check if the Array is empty before you attempt to get elements from it.我建议您在尝试从中获取元素之前检查 Array 是否为空。 I'm not quite sure which Array is throwing the error in your code as I can't tell what the database call does.我不太确定哪个 Array 在您的代码中抛出错误,因为我无法判断数据库调用的作用。 But I'd guess it's either dbData or mailTable.但我猜它要么是 dbData 要么是 mailTable。

Try something like this:尝试这样的事情:

if (dbData.length != 0) {
  //run your code here that access the elements
} else { //do nothing..or whatever you want to happen when it's empty, maybe 
error? }

Honestly i have no idea about def.老实说,我不知道def。 Is it some kind of lisp method?, So could it be that java doesn't understand index value, since it isn't a usual int index java variable.它是某种 lisp 方法吗?,所以可能是 java 不理解索引值,因为它不是通常的 int 索引 java 变量。 I might be wrong, i don't know about def method.我可能错了,我不知道 def 方法。 So sorry if i'm completely wrong.很抱歉,如果我完全错了。

Cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM