0

I'm writing a program that creates an infinitely long integer by making each number in the integer its own node.

For example: 123,456 = [1]->[2]->[3]->[4]->[5]->[6]

Also, I need to be able to add the contents of two infinite integers together.

For example: [1]->[2]->[3]->[4]->[5]->[6] + [1]->[2]->[3] = [1]->[2]->[3]->[5]->[7]->[9]

However, when I try to reversely traverse one of the linked lists, I get a NullPointerException after the first loop through;

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 is the linked list of the number 100000000000000000000. currentOne is a node initialized to the last node of the firstOperand. currentTwo is a node initialized to the last node of the secondOperand which has the contents of 000000000000000000001. The error is located in the line

                        if(currentOne.data > currentTwo.data)

The error occurs after only the second run through of the loop, and i'm not sure why, because when printed in string form, the secondOperand shows the full 000000000000000000001. So, I'm not sure why i'm getting the null pointer exception instead of currentTwo.data being equal to 0.

Here is the constructor for an InfiniteInteger object

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

This is the full method of where the error occurs:

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

}

I'm trying to subtract 1000000000000000 and 0000000000000001.

Here's my Node class:

    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);
    }
}
bkrause404
  • 19
  • 8
  • "Infinitely" long integer but 123,456 is a finite value. Interesting. – Ceelos Mar 15 '16 at 22:16
  • well "infinitely" long meaning that when the infiniteInteger object is created it can be created at any length. The value 1000000000000000 is just one instance. – bkrause404 Mar 15 '16 at 22:19
  • Posting your infiniteInteger class (relevant sections) code might help. Along with how you initialize your objects. – Ceelos Mar 15 '16 at 22:20

2 Answers2

1

The problem may happens because you fail to initialize the member variable data. Check your code carefully and debug it step by step.

Adam Lyu
  • 431
  • 1
  • 5
  • 17
0

Your constructor doesn't take care of member previous, or is this in the Node constructor?

Turo
  • 4,724
  • 2
  • 14
  • 27