Results 1 to 2 of 2
Thread: Null Pointer Exception
- 09-22-2008, 06:00 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 1
- Rep Power
- 0
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();
}
}
- 09-22-2008, 06:33 PM #2
Similar Threads
-
Null pointer Exception
By peiceonly in forum New To JavaReplies: 8Last Post: 09-05-2010, 06:48 PM -
null pointer exception
By cityguy503@yahoo.com in forum New To JavaReplies: 4Last Post: 08-22-2008, 07:22 PM -
getting a null pointer exception
By Rjava in forum XMLReplies: 4Last Post: 07-16-2008, 05:56 AM -
Null pointer Exception. after a bit of execution!! Plz help me
By rohan in forum Java AppletsReplies: 2Last Post: 05-01-2008, 10:14 AM -
Null pointer exception error
By brownie_jedi in forum New To JavaReplies: 3Last Post: 03-15-2008, 06:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks