0

I have this code:

public int[] KonversiIndexBM (int[] Individu) throws SQLException {

    int StringLenChromosome = Individu.length;
    ResultSet rs;
    int[] IndexBM = new int[StringLenChromosome];

    for (int j = 0; j < StringLenChromosome; j = j + 8) {
        IndexBM[j] = (int) Math.round(((this.CountRow(conn, "karbohidrt") - 1) * ((double) (Individu[j] - 1) / (55 - 1))) + 1);
        IndexBM[j + 1] = (int) Math.round((this.CountRow(conn, "protein_hewani") - 1) * ((double) (Individu[j + 1] - 1) / (55 - 1)) + 1);
        IndexBM[j + 2] = (int) Math.round((this.CountRow(conn, "protein_nabati") - 1) * ((double) (Individu[j + 2] - 1) / (55 - 1)) + 1);
        //IndexBM[j + 3] = (int) Math.round((this.CountRow(conn, "sayurana") - 1) * ((double) (Individu[j + 3] - 1) / (55 - 1)) + 1);
        IndexBM[j + 3] = (int) Math.round((this.CountRow(conn, "sayuranb") - 1) * ((double) (Individu[j + 3] - 1) / (55 - 1)) + 1);
        IndexBM[j + 4] = (int) Math.round((this.CountRow(conn, "buah") - 1) * ((double) (Individu[j + 4] - 1) / (55 - 1)) + 1);
        IndexBM[j + 5] = (int) Math.round((this.CountRow(conn, "lemak") - 1) * ((double) (Individu[j + 5] - 1) / (55 - 1)) + 1);
        IndexBM[j + 6] = (int) Math.round((this.CountRow(conn, "gula") - 1) * ((double) (Individu[j + 6] - 1) / (55 - 1)) + 1);
        IndexBM[j + 7] = (int) Math.round((this.CountRow(conn, "susu") - 1) * ((double) (Individu[j + 7] - 1) / (55 - 1)) + 1);
    }
    return IndexBM;
}

`

And the error message:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 189

The Line which Causes error is:

IndexBM[j + 5] = (int) Math.round((this.CountRow(conn, "lemak") - 1) * ((double) (Individu[j + 5] - 1) / (55 - 1)) + 1);

I don't know why this is wrong. What should I do?

Brick
  • 3,998
  • 8
  • 27
  • 47
  • 1
    You should read the stacktrace, see the line where the error occurs, then set a breakpoint and use the debugger. – OneCricketeer Jul 26 '16 at 22:06
  • Clearly, the issue is that for a certain value of j, j+5 is equal to the length of IndexBM. Since the length is determined by Individu, the array you're accepting as a paremeter is smaller than you think it is. – alexgbelov Jul 26 '16 at 23:17

0 Answers0