CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Stack List

  1. #1
    Join Date
    Apr 2009
    Posts
    2

    Stack List

    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.
    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; 
    }
    This is my output:
    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)
    any help would be great.

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Stack List

    The exception error message tells you where the problem is (StackList.java line 83).

    Unfortunately you haven't posted all the relevant code, and you haven't explained what operations you were doing when it failed, so it's hard to make any further comment...

    However, the peek method has an obvious bug - you check for lastNode being null before calling getData on it, but then you try to print out the data using the same call when it could be null.

    Controlling complexity is the essence of computer programming...
    B. Kernighan
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured