当列表转换为Big Integer时,它会抛出NullPointerExceptions吗?

时间:2015-11-21 13:29:40

标签: java algorithm

我正在为谷歌挑战创建一些代码,我需要编写以下功能:假设您有一个棋盘游戏x放置很长时间,并且你有一个三面染色,有'逗留','正确','和剩下'。我需要确定一个功能,当给定骰子数量和位数时,棋盘将返回多少种不同的方式,你可以用正确数量的骰子卷到达那里。 (当然没有走出无聊的边缘)然后Moodulas 123454321.(不要问我为什么我修改123454321的答案,我不知道)。我已经掀起了以下代码,但它抛出了一个空指针异常。我最初使用普通整数列表。但它们很小,无法通过所有测试。有人可以帮忙吗?这是代码:

public static void main(String args[]) {
        System.out.print(answer(100, 10));
    }



public static int answer(int diceRolls, int numOfSquares) {

        //Two many squares to be possible. Return 0 for 0 solutions.
        if ((numOfSquares - diceRolls) > 1) {
            return 0;
        }
        //If there are only two squares there is only One option. Roll a Right.
        if (numOfSquares == 2) {
            return diceRolls;
        }
        //If there are exactly as may dice rolls as there is squares. 
        //There are as much possibilities as there are squares
        //Therefore return number of squares.
        if (diceRolls == numOfSquares) {
            return numOfSquares;
        }

        //Tests how many options there are to roll and get as many dice rolls as needed. 
        //While still getting the amount of squares needed to finish the board game.
        BigInteger listOne[] = new BigInteger[numOfSquares]; //listOnes[current] holds the number valid paths to position 'current' using 'subCurrent-1' steps
        BigInteger listTwo[] = new BigInteger[numOfSquares]; //put number of valid paths to position 'current' using 'subCurrent' steps into listTwo[current]
        BigInteger total = new BigInteger("0");
        listOne[0] = BigInteger.valueOf(1);
        listOne[1] = BigInteger.valueOf(1);
        int max = 1;

        for (int index = 1; index < diceRolls; index++) {
            listTwo = new BigInteger[numOfSquares]; //Resets listTwo
            //Adds one to max until max is correctly fitted with number of squares
            if (max < (numOfSquares - 1)) {
                max++;
            }

            for (int subIndex = 0; subIndex < numOfSquares && subIndex < (max + 1); subIndex++) {
                //If at first square
                if (subIndex == 0) {
                    //Set listTwo's current value to lstOnes current value + the next value.
                listTwo[subIndex] = listOne[subIndex].add(listOne[subIndex + 1]);
                } else if (subIndex == max) {
                    //If at final square
                    if (subIndex == (numOfSquares - 1)) {
                        total.add(listOne[subIndex - 1]); //Adds listOnes last value to total.
                    } else {
                         listTwo[subIndex] = listOne[subIndex - 1];
                    } else {
                        //If subIndex is not yet at final or first Square
                        //Set current value of listTwo to listOne's last square + current square + nextSquare
                        listTwo[subIndex] = listOne[subInde add(listOne[subIndex].add(listOne[subIndex + 1]));
                    }
                }
                listOne = listTwo; //Sets listOne to the new value of listTwo
            }

            int answer = total.mod(BigInteger.valueOf(123454321)).intValue(); //For some reason question asks to return modulo 123454321 of the number
            return answer;
        }

现在这里是堆栈跟踪:

Exception in thread "main" java.lang.NullPointerException
    at java.math.BigInteger.add(Unknown Source)
    at arrays.MinionsBoredGames.answer(MinionsBoredGames.java:59)
    at arrays.MinionsBoredGames.main(MinionsBoredGames.java:8)

0 个答案:

没有答案