无限整数链表NullPointerException

时间:2016-03-15 22:08:54

标签: java nullpointerexception

我正在编写一个程序,通过使整数中的每个数字成为自己的节点来创建一个无限长的整数。

例如:123,456 = [1] - > [2] - > [3] - > [4] - > [5] - > [6]

另外,我需要能够将两个无限整数的内容一起添加。

例如:[1] - > [2] - > [3] - > [4] - > [5] - > [6] + [1] - > [2] - > [3] = [1] - > [2] - > [3] - > [5] - > [7] - > [9]

然而,当我尝试反向遍历其中一个链表时,我在第一次循环后得到NullPointerException;

while((currentOne != null) && (currentTwo != null)) {
    // for(int i = firstOperand.getNumberOfDigitsWithZeroes(); i>0; i--)

    if(currentOne.data > currentTwo.data) {
        tempResult = currentOne.data - currentTwo.data;
        //result = tempResult + result;
        returnResult.addToFront(tempResult);
    } else if(currentOne.data < currentTwo.data) {
        currentOne.previous.data -= 1;
        currentOne.data += 10;
        tempResult = currentOne.data - currentTwo.data;
        //result = tempResult + result;
        returnResult.addToFront(tempResult);
    } else if((currentOne == firstOperand.firstNode && currentTwo == secondOperand.firstNode) && 
            currentOne.data - currentTwo.data == 0) {
        break;
    } else {
        returnResult.addToFront(0);
    }
    currentOne = currentOne.previous;
    currentTwo = currentTwo.previous;
}

firstOperand是数字100000000000000000000的链接列表.currentOne是初始化为firstOperand的最后一个节点的节点。 currentTwo是初始化为secondOperand最后一个节点的节点,其内容为000000000000000000001.错误位于行

                        if(currentOne.data > currentTwo.data)

错误发生在循环的第二次运行后,我不知道为什么,因为当以字符串形式打印时,secondOperand显示完整的000000000000000000001.所以,我不知道为什么我得到空指针异常而不是currentTwo.data等于0。

这是InfiniteInteger对象的构造函数

public LInfiniteInteger(String s)
{
    // TO DO
    int newDigit = 0;
    isNegative = false;
    numberOfDigits = 0;
    middlePosition = 0;
    firstNode = null;
    middleNode = null;
    lastNode =null;
    for(int i =0; i<s.length(); i++)
    {
        newDigit = (int)s.charAt(i) - 48;
        if((newDigit >= 0) && (newDigit <= 9))
        {
            //  this.add(newDigit);
            if(firstNode == null)
            {
                firstNode = new Node(null, newDigit, null);
                lastNode = firstNode;
            }
            else
            {
                Node newNode  = new Node(lastNode, newDigit, null);
                lastNode.next = newNode;
                lastNode = newNode;
            }
            numberOfDigits++;

            if(numberOfDigits %2 == 1);
            {
                if(middleNode == null)
                {
                    middleNode = firstNode;
                }
                else
                {
                    middleNode = middleNode.next;
                }
                middlePosition++;
            }
        }
        else if((newDigit == ((int)'-') - 48))
        {
            isNegative = true;
        }
    }
}

这是发生错误的完整方法:

    public InfiniteIntegerInterface minus(final InfiniteIntegerInterface anInfiniteInteger)
{
                // TO DO
        LInfiniteInteger firstOperand = new LInfiniteInteger(this.toString());
        LInfiniteInteger secondOperand = new LInfiniteInteger(anInfiniteInteger.toString());
        LInfiniteInteger returnResult = new LInfiniteInteger("");
        int tempResult;
        boolean resultIsNegative = false;
        Node currentOne = firstOperand.lastNode;
        Node currentTwo = secondOperand.lastNode;


        while(firstOperand.getNumberOfDigitsWithZeroes() > secondOperand.getNumberOfDigitsWithZeroes())
        {
            Node temp = new Node(0);
            temp.next=secondOperand.firstNode;
            secondOperand.firstNode = temp;
            secondOperand.addNumberOfDigits();

        }
        while(firstOperand.getNumberOfDigitsWithZeroes() < secondOperand.getNumberOfDigitsWithZeroes())
        {
            Node temp = new Node(0);
            temp.next=firstOperand.firstNode;
            firstOperand.firstNode = temp;
            firstOperand.addNumberOfDigits();
        }

        if((firstOperand.isNegative == false) && (secondOperand.isNegative == false))
        {
                if(firstOperand.compareMag(secondOperand) == 1)
                {
                        //algorithm
                        //System.out.println(currentTwo.data);
                        //System.out.println(secondOperand.toString());
                        for(int i = secondOperand.getNumberOfDigitsWithZeroes(); i > 0; i--)
                        {
                            System.out.println(currentOne.data);
                            currentOne = currentOne.previous;
                        }
                        currentOne = firstOperand.lastNode;
                        while((currentOne != null) && (currentTwo != null))
                        // for(int i = firstOperand.getNumberOfDigitsWithZeroes(); i>0; i--)
                        {
                                if(currentOne.data > currentTwo.data)
                                {
                                        tempResult = currentOne.data - currentTwo.data;
                                        //result = tempResult + result;
                                        returnResult.addToFront(tempResult);
                                }
                                else if(currentOne.data < currentTwo.data)
                                {
                                        currentOne.previous.data -= 1;
                                        currentOne.data += 10;
                                        tempResult = currentOne.data - currentTwo.data;
                                        //result = tempResult + result;
                                        returnResult.addToFront(tempResult);
                                }
                                else if((currentOne == firstOperand.firstNode && currentTwo == secondOperand.firstNode) && currentOne.data - currentTwo.data == 0)
                                {
                                    break;
                                }
                                else
                                {
                                    returnResult.addToFront(0);
                                }
                                currentOne = currentOne.previous;
                                currentTwo = currentTwo.previous;
                        }
                        if(currentOne == null)
                        {
                                while(currentTwo != null)
                                {
                                        //result = currentTwo.data + result;
                                        returnResult.addToFront(currentTwo.data);
                                        currentTwo = currentTwo.previous;
                                }
                        }
                        else if(currentTwo == null)
                        {
                                while(currentTwo != null)
                                {
                                        //result = currentTwo.data + result;
                                        returnResult.addToFront(currentTwo.data);
                                        currentTwo = currentTwo.previous;
                                }
                        }

                        return returnResult;
                }
                else if(firstOperand.compareMag(secondOperand) == 0)
                {
                        returnResult.add(0);
                        return returnResult;
                }
                else
                {
                        LInfiniteInteger tempReturnResult = new LInfiniteInteger(secondOperand.minus(firstOperand).toString());
                        Node currentOneInner = tempReturnResult.firstNode;
                        while(currentOneInner != null)
                        {
                                returnResult.add(currentOneInner.data);
                                currentOneInner = currentOneInner.next;
                        }
                        returnResult.isNegative = true;
                        return returnResult;
                }
        }
        else if((firstOperand.isNegative == false) && (secondOperand.isNegative == true))
        {
                secondOperand.isNegative = false;
                LInfiniteInteger tempReturnResult = new LInfiniteInteger(firstOperand.plus(secondOperand).toString());
                Node currentOneInner = tempReturnResult.firstNode;
                while(currentOneInner != null)
                {
                        returnResult.add(currentOneInner.data);
                        currentOneInner = currentOneInner.next;
                }
                return returnResult;
        }
        else if((firstOperand.isNegative == true) && (secondOperand.isNegative == false))
        {
                firstOperand.isNegative = false;
                LInfiniteInteger tempReturnResult = new LInfiniteInteger(firstOperand.plus(secondOperand).toString());
                Node currentOneInner = tempReturnResult.firstNode;
                while(currentOneInner != null)
                {
                        returnResult.add(currentOneInner.data);
                        currentOneInner = currentOneInner.next;
                }
                returnResult.isNegative = true;
                return returnResult;
        }
        else
        {
                if(firstOperand.compareMag(secondOperand) == -1)
                {
                        firstOperand.isNegative = false;
                        secondOperand.isNegative = false;
                        LInfiniteInteger tempReturnResult = new LInfiniteInteger(firstOperand.minus(secondOperand).toString());
                        Node currentOneInner = tempReturnResult.firstNode;
                        while(currentOneInner != null)
                        {
                                returnResult.add(currentOneInner.data);
                                currentOneInner = currentOneInner.next;
                        }
                        return returnResult;
                }
                else if(firstOperand.compareMag(secondOperand) == 0)
                {
                        returnResult.add(0);
                        return returnResult;
                }
                else
                {
                        firstOperand.isNegative = false;
                        secondOperand.isNegative = false;
                        LInfiniteInteger tempReturnResult = new LInfiniteInteger(firstOperand.minus(secondOperand).toString());
                        Node currentOneInner = tempReturnResult.firstNode;
                        while(currentOneInner != null)
                        {
                                returnResult.add(currentOneInner.data);
                                currentOneInner = currentOneInner.next;
                        }
                        returnResult.isNegative = true;
                        return returnResult;
                }
        }

}

我正在尝试减去1000000000000000和0000000000000001。

这是我的Node类:

    private class Node
{
    private int data;
    private Node next;
    private Node previous;

    private Node(Node previousNode, int aData, Node nextNode)
    {
        previous = previousNode;
        data = aData;
        next = nextNode;
    }

    private Node(int aData)
    {
        this(null, aData, null);
    }
}

2 个答案:

答案 0 :(得分:1)

可能会发生此问题,因为您无法初始化成员变量数据。仔细检查您的代码并逐步调试它。

答案 1 :(得分:0)

您的构造函数不会处理之前的成员,或者是否在Node构造函数中?