Results 1 to 2 of 2
  1. #1
    demiser55 is offline Member
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Default Null Pointer Exception

    package ch03.stacks;

    import support.LLObjectNode;

    public class LinkedStack implements UnboundedStackInterface

    {
    protected LLObjectNode top; // reference to the top of this stack

    public LinkedStack()
    {
    top = null;
    }

    public void push(Object element)
    // Places element at the top of this stack.
    {
    LLObjectNode newNode = new LLObjectNode(element);
    newNode.setLink(top);
    top = newNode;
    }

    public void pop()
    // Throws StackUnderflowException if this stack is empty,
    // otherwise removes top element from this stack.
    {
    if (!isEmpty())
    {
    top = top.getLink();
    }
    else
    throw new StackUnderflowException("Pop attempted on an empty stack.");
    }

    public Object top()
    // Throws StackUnderflowException if this stack is empty,
    // otherwise returns top element from this stack.
    {
    if (!isEmpty())
    return top.getInfo();
    else
    throw new StackUnderflowException("Top attempted on an empty stack.");
    }

    public boolean isEmpty()
    // Returns true if this stack is empty, otherwise returns false.
    {
    if (top == null)
    return true;
    else
    return false;
    }

    public Object inspector (int n)
    {

    //inserts 1 2 3 4 for parameter but at 3 displays Null Pointer Exception error
    how would i fix this?

    //basically inspects linked stack to see information exists and if it does returns it otherwise returns null.

    LLObjectNode currNode=top;
    int count=1;

    while(count<n && top!=null)
    {
    count+=1;
    top=top.getLink();
    }

    if(top.getInfo()==null)
    {
    return null;
    }
    else

    return top.getInfo();
    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,275
    Rep Power
    25

    Default

    Do you have a question?
    If you get errors, copy and paste the full text of the message here.

    What variable at code where the NPE occurs could be null? Use println() to show the values of all the variables used at that statement.

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 07:22 PM
  3. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 05:56 AM
  4. Replies: 2
    Last Post: 05-01-2008, 10:14 AM
  5. Null pointer exception error
    By brownie_jedi in forum New To Java
    Replies: 3
    Last Post: 03-15-2008, 06:27 AM

Posting Permissions

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