0

I am writing a stack program in Java. In the code the push function is causing a null pointer exception. I guess the node is not getting created. Please advice. Thanks in advance

//Stack_using_ll is a stack implementation using linked list 
public class Stack_using_ll{
    private Node first;
    private int count;
    private class Node {
        private String str;
        private Node next;
    }// Node has a value and reference

    public void push(String item){
        Node old_first = first;
        first = new Node();
        first.str = item;
        first.next = old_first.next;
        //first = node;
        count++;
    }//Inserts a new node
    public String pop(){
        String str_pop = first.str;
        first = first.next;
        count--;
        return str_pop;
    }//pops the string out of the stack
    public boolean is_empty(){
        if(first == null)
            return true;
        else
            return false;
    }//check if the stack is empty

    public static void main(String[] args){
        Stack_using_ll stack = new Stack_using_ll() ;
        stack.push("Jeans");
        System.out.println("There are " + stack.count + " elements in stack");
    }
}//End of class Stack_using_ll    

-------------The output i get is as follows-----------------------------

java.lang.NullPointerException
    at Stack_using_ll$Node.access$2(Stack_using_ll.java:7)
    at Stack_using_ll.push(Stack_using_ll.java:14)
    at Stack_using_ll.main(Stack_using_ll.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
1
  • You can just use LinkedList for this. Commented Oct 14, 2013 at 13:25

4 Answers 4

1

In your code:

public class Stack_using_ll{
    private Node first;

private Node first only declares a reference to a Node named 'first', it does not create an instance of a Node for it to refer to.

So, when you later assign first to oldFirst, you're assigning a null, attempting to access a member of a null results in a NPE

public void push(String item){
        Node old_first = first;
        first = new Node();
        first.str = item;
        first.next = old_first.next; <- NPE here
Sign up to request clarification or add additional context in comments.

Comments

0

Within your main method you create a new Stack_using_ll object, this creates any member variables, however you never give first a value (e.g. within the constructor), as such it remains null.

private Node first; //<--- first is null here and you use the blank constructor, so it is never given a non null value

You then call stack.push("Jeans"); in your main method which attempts to use first, but first is null, hence the exception.

public void push(String item){
    Node old_first = first; //<-- the initial null is preserved here
    first = new Node();
    first.str = item;
    first.next = old_first.next; //<-- you attempt to use the preserved null here
    //first = node;
    count++;
}

Comments

0

problem is the null pointer in the first time you push to you object (because first item is null and you try to get its .next

see correction below : (in push function )

public void push(String item){
    Node old_first = first;
    first = new Node();
    first.str = item;
    //first time old_first is null!
    if (old_first != null){
       first.next = old_first.next;
    }
    //first = node;
    count++;
}

Comments

0

first is null at the beginning so when you do: old_first = first, old_first becomes null, so old_first.next gives you exception.

Solution:

 public void push(String item){
        Node old_first = first;
        first = new Node();
        first.str = item;
        if(old_first!=null)
           first.next = old_first.next;
    ...}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.