内部编译器错误?

时间:2016-11-22 17:11:32

标签: java compiler-errors

目前这段代码还没有完成,我只是编写了随机生成一个3x3数组所需的方法,每个空格中有一个不同的单个数字。但是,我在Internal compiler error: java.lang.NullPointerException at org.eclipse.jdt.internal.compiler.ast.ReferenceExpression.copy(ReferenceExpression.java:141)的第一行收到错误。我不太确定这个错误意味着什么,因为我只是一个初出茅庐的程序员。我该如何解决这个错误?

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

}

2 个答案:

答案 0 :(得分:3)

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

意味着Eclipse Java Compiler的实际内部内容非常混乱,以至于它们取消引用了空指针。根据定义,这是Eclipse中的一个错误。最有可能的是,你因为代码中的一些错误而遇到了这个错误;如果一个小的,正确的Java类击中其中一个,那将是非常令人惊讶的。

您可以尝试使用真正的JDK编译代码,它可能会告诉您需要修复的内容。通常,您不应该依赖IDE作为唯一的构建工具;有一天你会希望从命令行拥有可重复的构建,并且你想学习ant,Maven或gradle。也许各种编译网站中的一个可以方便您的直接需求。

答案 1 :(得分:0)

您的代码存在一些编译问题。此外,您的代码将在下面的代码片段中遇到无限循环。 Math.random()生成的值小于1.假设Math.random()生成的最大值为0.999,则(Math.random() * 8)的结果为7.992(Math.random() * 8)的结果永远不会等于或大于8.所以(int) (Math.random() * 8 + 1)的最大值永远不会大于8,因为你不是(Math.random() * 8)的四舍五入,如果{转换为整数。

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

我找到了这样一个错误的引用https://bugs.eclipse.org/bugs/show_bug.cgi?id=482737我不确定问题是否与您的情况相同。

我对您的代码进行了一些修改,这些修改可以成功编译并运行。

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