0

Here is the segment of my code The add method functions as an arrayList add method to add a value to a specified index. The size variable increases when declaring this method in the runner, but displaying the new list results in a NullPointException.

public void add(int index, E value) {
        ListNode<E> newNode = new ListNode <E>(value);
        ListNode <E> tempNode = root;
        if(index==0 && root==null) {
            root.setPrevious(newNode);
            root = root.previous;
        }

        if(index == size) {
            end.setNext(newNode);
            end = end.next;
        }

        if(index>0 && index<size) {
            for (int i = 0; i < index-1 ; i++) {
                tempNode = tempNode.next;
            }
            tempNode.next = newNode;
        }
        size++;
    }

  public String toString() {
        String output = "";
        ListNode<E> tempNode = root;
        for(int i=0 ; i<size ; i++) {
            output+=tempNode.getValue();//figure out why tempNode can be null
            if(i!=size-1)
                output+=", ";
            tempNode = tempNode.next;
        }
        return output;
    }
  • how are you populating the list? – LZR Nov 23 '20 at 18:45
  • @pavel.lazar my runner calls, SuperList list = new SuperList<>();. Each value is then added using a default add function. Ex. list.add(2);. The modified list can then be printed out in a System.out statement. – hyPe_Coder Nov 23 '20 at 18:49

0 Answers0