0

I am writing a code using JavaFX that works with a Canvas to create fill Rectangles.

This code is based off of some physics research that I am doing.

The issue I am running into comes about when I run a nested for loop with a lot of logical if statements.

The code works for most cases, but doesn't work when isAngMomConservation == false and isParityConservation == false.

Here is the code:

for (int i = 0, k = finals - 1, finalVal = (finals - 1) / 2 * -1; i < finals; i++, k--, finalVal++) {
    for (int j = 0, initialVal = (initial - 1) / 2 * -1; j < initial; j++, initialVal++) {
        final int ii = i;
        final int jj = j;
        System.out.println("i: " + ii + ", j: " + jj);

        if (isHelicity == false) {
            if (isParityConservation && isAngMomConservation == false) {
                setisDependent(false, i, j);
                if (parity == 1 && ((i % 2 == 0 && orbital % 2 == 1) || (i % 2 == 1 && orbital % 2 == 0))) {
                    setisAllowed(true);
                }
                if (parity == -1 && ((i % 2 == 0 && orbital % 2 == 0) || (i % 2 == 1 && orbital % 2 == 1))) {
                    setisAllowed(true);
                }
            } else if (isAngMomConservation && isParityConservation == false) {
                if (i == j) {
                    setisAllowed(true);
                }
            } else if (isAngMomConservation && isParityConservation) {
                if (parity == 1 && i == j
                        && ((i % 2 == 0 && orbital % 2 == 1) || (i % 2 == 1 && orbital % 2 == 0))) {
                    setisAllowed(true);
                }
                if (parity == -1 && i == j
                        && ((i % 2 == 0 && orbital % 2 == 0) || (i % 2 == 1 && orbital % 2 == 1))) {
                    setisAllowed(true);
                }
            } else {
                setisAllowed(true);
            }
            if (isTimeReversal && isAngMomConservation == false) {
                if (j <= k) {
                    setisDependent(false, i, j);
                    setisDependent2(false);
                } else {
                    setisDependent(true, i, j);
                    setisDependent2(true);
                }
            } else if (isTimeReversal && isAngMomConservation) {
                if (j <= k) {
                    setisDependent(false, i, j);
                    setisDependent2(false);
                }
                if (i == j && j > k) {
                    setisDependent(true, i, j);
                    setisDependent2(true);
                }
            }
            if (isTimeReversal == false) {
                setisDependent(false, i, j);
                setisDependent2(false);
            }

        } else {
            if (isAngMomConservation && isParityConservation == false) {
                if (j / helicity == i) {
                    setisAllowed(true);
                }
            }
            if (isAngMomConservation == false && isParityConservation) {

            }
        }

        if ((i % 2 == 0 && orbital % 2 == 1) || (i % 2 == 1 && orbital % 2 == 0)) {
            gc[i][j].setFill(colors[0]);
        } else {
            gc[i][j].setFill(colors[1]);
        }
        if (isAllowed == false) {
            gc[i][j].setFill(colors[2]);
        }
        gc[i][j].fillRect((j % initial) * BOX_SIZE, (i % finals) * BOX_SIZE, BOX_SIZE, BOX_SIZE);
        gc[i][j].setStroke(colors[3]);
        gc[i][j].setLineWidth(1);
        gc[i][j].strokeRect((j % initial) * 50, (i % finals) * 50, 50, 50);

        if (isAllowed && isDependent2 == false) {
            gc[i][j].setFill(colors[2]);
            gc[i][j].setFont(new javafx.scene.text.Font(30));
            gc[i][j].fillText("X", ((j % initial) * 50) + 15, ((i % finals) * 50) + 35, 50);
        }
        setisAllowed(false);
    }
}

I hope that I have provided enough code for my question to be answered. I have setter methods that allow me to set the values of all the booleans. Thanks for taking a look at my code.

DVarga
  • 19,000
  • 5
  • 49
  • 55
  • Which line throws the exception? – SurvivalMachine Jun 30 '16 at 06:25
  • No you haven't provided enough code. You're posting code, don't tell us where exactly the exception happens and the only place in that code that could cause the OutOfBoundsException (unless it's thrown from one of the methods that you haven't posted) is `gc[i][j]`. However you haven't posted the creation of the array (and the way you fill it, in case you create the inner arrays "manually", e.g. like `gc[index] = `). All we can tell you with this amount of information is: `finals` and/or `initial` are probably too large... – fabian Jun 30 '16 at 08:24
  • 1
    Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – fabian Jun 30 '16 at 08:24

0 Answers0