Jay Weathington
Jay Weathington

Reputation: 21

Internal Compiler error?

Currently this code is not finished, I'm just writing the methods needed to randomly generate a 3x3 array with a different single digit number in each space. However, I'm getting an error on the first line that says Internal compiler error: java.lang.NullPointerException at org.eclipse.jdt.internal.compiler.ast.ReferenceExpression.copy(ReferenceExpression.java:141). I'm not really sure what this error means, as I'm only a fledgling programmer. How can I fix this error?

public class MagicSquare {
    public static void main(String[] args) {

    }

    public static int sumRows(int[][] array) {
        int totalRowOne;
        int totalRowTwo;
        int totalRowThree;

        totalRowOne = array[0][0] + array[0][1] + array[0][2];
        totalRowTwo = array[1][0] + array[1][1] + array[1][2];
        totalRowThree = array[2][0] + array[2][1] + array[2][2];

        if(totalRowOne == totalRowTwo && totalRowOne == totalRowThree) {
            return totalRowOne;
        }
        else {
            return -1;
        }
    }

    public static int sumColumns(int[][] array) {
        int totalColumnOne;
        int totalColumnTwo;
        int totalColumnThree;

        totalColumnOne = array[0][0] + array[1][0] + array[2][0];
        totalColumnTwo = array[0][1] + array[1][1] + array[2][0];
        totalColumnThree = array[0][2] + array[1][2] + array[2][2];

        if(totalColumnOne == totalColumnTwo && totalColumnOne == totalColumnThree) {
            return totalColumnOne;
        }
        else {
            return -1;
        }
    }

    public static int sumDiagonals(int[][] array) {
        int totalDiagonalOne;
        int totalDiagonalTwo;

        totalDiagonalOne = array[0][0] + array[1][1] + array[2][2];
        totalDiagonalTwo = array[0][2] + array[1][1] + array[2][0];

        if(totalDiagonalOne == totalDiagonalTwo) {
            return totalDiagonalOne;
        }
        else {
            return -1;
        }
    }

    public static boolean isUnique(int[][] array, int num) {
        if(num == array[0][0] || num == array[0][1] || num == array[0][2] ||
           num == array[1][0] || num == array[1][1] || num == array[1][2] ||
           num == array[2][0] || num == array[2][1] || num == array[2][2]) {
            return true;
        }
        else {
            return false;
        }
    }

    public static void displayMagicSquare(int[][] array) {
        int counter1;
        int counter2;

        for(counter1 = 0; counter1 < 3; counter1++) {
            for(counter2 = 0; counter2 < 3; counter2++) {
                System.out.print(array[counter1][counter2]);
            }
            System.out.print("/n");
        }
    }

    public static int[][] fillMatrix(int[][] array) {
        int numberOne;
        int numberTwo;
        int numberThree;
        int numberFour;
        int numberFive;
        int numberSix;
        int numberSeven;
        int numberEight;
        int numberNine;

        array[0][0] = 0;
        array[0][1] = 0;
        array[0][2] = 0;
        array[1][0] = 0;
        array[1][1] = 0;
        array[1][2] = 0;
        array[2][0] = 0;
        array[2][1] = 0;
        array[2][2] = 0;

        numberOne = (int)(Math.random() * 8 + 1);
        do {
            numberTwo = (int)(Math.random() * 8 + 1);
        } while(numberTwo == numberOne);

        do {
        numberThree = (int)(Math.random() * 8 + 1);
        } while(numberThree == numberTwo || numberThree == numberOne);

        do {
        numberFour = (int)(Math.random() * 8 + 1);
        } while(numberFour == numberThree || numberFour == numberTwo || numberFour == numberOne);

        do {    
        numberFive = (int)(Math.random() * 8 + 1);
        } while (numberFive == numberFour || numberFive == numberThree || numberFive == numberTwo ||
                 numberFive == numberOne);

        do {
        numberSix = (int)(Math.random() * 8 + 1);
        } while(numberSix == numberFive || numberSix == numberFour || numberSix == numberThree ||
                numberSix == numberTwo ||numberSix == numberOne);

        do {
        numberSeven = (int)(Math.random() * 8 + 1);
        } while(numberSeven == numberSix || numberSeven == numberFive || numberSeven == numberFour ||
                numberSeven == numberThree ||numberSeven == numberTwo ||numberSeven == numberOne);

        do {
        numberEight = (int)(Math.random() * 8 + 1);
        } while(numberEight == numberSeven || numberEight == numberSix || numberEight == numberFive ||
                numberEight == numberFour ||numberEight == numberThree ||numberEight == numberTwo ||
                numberEight == numberOne);

        do {
        numberNine = (int)(Math.random() * 8 + 1);
        } while(numberNine == numberEight || numberNine == numberSeven || numberNine == numberSix ||
                numberNine == numberFive || numberNine == numberFour ||numberNine == numberThree ||
                numberNine == numberTwo || numberNine == numberOne);

        array[0][0] = numberOne;
        array[0][1] = numberTwo;
        array[0][2] = numberThree;
        array[1][0] = numberFour;
        array[1][1] = numberFive;
        array[1][2] = numberSix;
        array[2][0] = numberSeven;
        array[2][1] = numberEight;
        array[2][2] = numberNine;

        return array;
    }

    public static boolean isMagicSquare(int[][] array) {
        if(sumRows(int[][] array) == sumColumns(int[][] array) && sumColumns(int[][] array) == sumDiagonals(int[][] array)) {
            return true;
        }
        else {
            return false;
        }
    }

}

Upvotes: 1

Views: 2674

Answers (2)

bmargulies
bmargulies

Reputation: 100186

Internal compiler error: java.lang.NullPointerException at org.eclipse.jdt.internal.compiler.ast.ReferenceExpression.copy(ReferenceExpression.java:141)

means that the actual internal guts of the Eclipse Java Compiler got so confused that they dereferenced a null pointer. This is, by definition, a bug in Eclipse. Most likely, you hit this bug because of some error in your code; it would be very surprising if a small, correct, Java class hit one of these.

You can try compiling your code with the real JDK, it's likely to tell you what you need to fix. In general, you should not depend on an IDE as your only build tool; some day you will want to have repeatable builds from command line, and you'll want to learn ant, or Maven, or gradle. Perhaps one of the various compiling web sites would be convenient for your immediate needs.

Upvotes: 3

kk.
kk.

Reputation: 3945

There were some compilation issues with your code. Also, your code will run into infinite loop for the below code snippet. Math.random() generates value less than 1. Suppose Math.random() generates max value as 0.999 then the result of (Math.random() * 8) would be 7.992. The result of (Math.random() * 8) would never be equal to or greater than 8. So the max value of (int) (Math.random() * 8 + 1) will never be greater than 8 because you are not rounding of (Math.random() * 8) which would be 7 if converted to integer.

do {
    numberNine = (int) (Math.random() * 8 + 1);
} while(numberNine == numberEight || numberNine == numberSeven || numberNine == numberSix || numberNine == numberFive || numberNine == numberFour || numberNine == numberThree || numberNine == numberTwo || numberNine == numberOne);

I have found a reference for such an error https://bugs.eclipse.org/bugs/show_bug.cgi?id=482737 I am not sure whether the problem is same with your case.

I have made some modifications to your code which compiles and runs successfully.

public class MagicSquare {
    public static void main(String[] args) {
        int[][] array = new int[3][3];
        fillMatrix(array);
        System.out.println("Is magical square:" + isMagicSquare(array));
        displayMagicSquare(array);
        System.out.println("Is number present:" + isUnique(array, 1));
    }

    private static int sumRows(int[][] array) {
        int totalRowOne;
        int totalRowTwo;
        int totalRowThree;

        totalRowOne = array[0][0] + array[0][1] + array[0][2];
        totalRowTwo = array[1][0] + array[1][1] + array[1][2];
        totalRowThree = array[2][0] + array[2][1] + array[2][2];

        if(totalRowOne == totalRowTwo && totalRowOne == totalRowThree) {
            return totalRowOne;
        } else {
            return -1;
        }
    }

    private static int sumColumns(int[][] array) {
        int totalColumnOne;
        int totalColumnTwo;
        int totalColumnThree;

        totalColumnOne = array[0][0] + array[1][0] + array[2][0];
        totalColumnTwo = array[0][1] + array[1][1] + array[2][0];
        totalColumnThree = array[0][2] + array[1][2] + array[2][2];

        if(totalColumnOne == totalColumnTwo && totalColumnOne == totalColumnThree) {
            return totalColumnOne;
        } else {
            return -1;
        }
    }

    private static int sumDiagonals(int[][] array) {
        int totalDiagonalOne;
        int totalDiagonalTwo;

        totalDiagonalOne = array[0][0] + array[1][1] + array[2][2];
        totalDiagonalTwo = array[0][2] + array[1][1] + array[2][0];

        if(totalDiagonalOne == totalDiagonalTwo) {
            return totalDiagonalOne;
        } else {
            return -1;
        }
    }

    private static boolean isUnique(int[][] array, int num) {
        if(num == array[0][0] || num == array[0][1] || num == array[0][2] || num == array[1][0] || num == array[1][1] || num == array[1][2] || num == array[2][0] || num == array[2][1] || num == array[2][2]) {
            return true;
        } else {
            return false;
        }
    }

    private static void displayMagicSquare(int[][] array) {
        int counter1;
        int counter2;

        for(counter1 = 0; counter1 < 3; counter1++) {
            for(counter2 = 0; counter2 < 3; counter2++) {
                System.out.print(array[counter1][counter2] + "\t");
            }
            System.out.print("\n");
        }
    }

    private static int[][] fillMatrix(int[][] array) {
        int numberOne;
        int numberTwo;
        int numberThree;
        int numberFour;
        int numberFive;
        int numberSix;
        int numberSeven;
        int numberEight;
        int numberNine;

        array[0][0] = 0;
        array[0][1] = 0;
        array[0][2] = 0;
        array[1][0] = 0;
        array[1][1] = 0;
        array[1][2] = 0;
        array[2][0] = 0;
        array[2][1] = 0;
        array[2][2] = 0;

        numberOne = (int) (Math.random() * 8 + 1);
        do {
            numberTwo = (int) (Math.random() * 8 + 1);
        } while(numberTwo == numberOne);

        do {
            numberThree = (int) (Math.random() * 8 + 1);
        } while(numberThree == numberTwo || numberThree == numberOne);

        do {
            numberFour = (int) (Math.random() * 8 + 1);
        } while(numberFour == numberThree || numberFour == numberTwo || numberFour == numberOne);

        do {
            numberFive = (int) (Math.random() * 8 + 1);
        } while(numberFive == numberFour || numberFive == numberThree || numberFive == numberTwo || numberFive == numberOne);

        do {
            numberSix = (int) (Math.random() * 8 + 1);
        } while(numberSix == numberFive || numberSix == numberFour || numberSix == numberThree || numberSix == numberTwo || numberSix == numberOne);

        do {
            numberSeven = (int) (Math.random() * 8 + 1);
        } while(numberSeven == numberSix || numberSeven == numberFive || numberSeven == numberFour || numberSeven == numberThree || numberSeven == numberTwo || numberSeven == numberOne);

        do {
            numberEight = (int) (Math.random() * 8 + 1);
        } while(numberEight == numberSeven || numberEight == numberSix || numberEight == numberFive || numberEight == numberFour || numberEight == numberThree || numberEight == numberTwo || numberEight == numberOne);

        do {
            numberNine = (int) (Math.random() * 8 + 2);
        } while(numberNine == numberEight || numberNine == numberSeven || numberNine == numberSix || numberNine == numberFive || numberNine == numberFour || numberNine == numberThree || numberNine == numberTwo || numberNine == numberOne);

        array[0][0] = numberOne;
        array[0][1] = numberTwo;
        array[0][2] = numberThree;
        array[1][0] = numberFour;
        array[1][1] = numberFive;
        array[1][2] = numberSix;
        array[2][0] = numberSeven;
        array[2][1] = numberEight;
        array[2][2] = numberNine;

        return array;
    }

    private static boolean isMagicSquare(int[][] array) {
        if(sumRows(array) == sumColumns(array) && sumColumns(array) == sumDiagonals(array)) {
            return true;
        }
        else {
            return false;
        }
    }
}

Upvotes: 0

Related Questions