Im trying to make a stack with its top entry at the end of a chain of linked nodes. When I run it, I get a null pointer exception.
This is my output:Code:public class StackList<T> implements StackInterface<T> { private Node firstNode; // reference to first node private Node lastNode; // reference to last node private int length = 0; // number of entries in list public StackList(){ firstNode = null; lastNode = null; } public void push(T newEntry){ Node newNode = new Node (newEntry, lastNode, firstNode); if(length<1) { firstNode = newNode; lastNode = firstNode; }else {lastNode = newNode; }length++; } public T pop(){ { T result = peek (); if (lastNode != null) assert !isEmpty (); if (length == 1) { firstNode = firstNode.next; lastNode = null; // solitary entry was removed } else { Node nodeBefore = GetNodeAt (length - 1); Node nodeToRemove = nodeBefore.next; Node nodeAfter = nodeToRemove.next; nodeBefore.next = nodeAfter; // disconnect node to be removed lastNode = nodeBefore; // last node was removed } // end if length--; return result; } } public T peek(){ T result = null; if (lastNode != null) result = lastNode.getData(); System.out.print(lastNode.getData() +" "); return result; }any help would be great.Code:10 hello blah Exception in thread "main" java.lang.NullPointerException at StackList$Node.access$1(StackList.java:83) at StackList.pop(StackList.java:42) at StackList.main(StackList.java:114)




Reply With Quote