Q:

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>";

A:

You're getting the Array Index OutOfBoundsException because you're trying to access element in an empty Array (0 elements) most likely. I'd suggest you check if the Array is empty before you attempt to get elements from it.
I'm not quite sure which Array is throwing the error in your code as I can't tell what the database call does. But I'd guess it's either dbData or 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? }

