0

I am new at java and I am trying to write a Linked-List Stack..

public class Stack {

    private Node first;

    private class Node {
        int item;
        Node next;
    }

    public boolean IsEmpty()
    {
        return first==null;
    }

    public void push(int item)
    {
        Node oldfirst=first;
        first=new Node();
        first.item=item;
        first.next=oldfirst;
    }

    public int pop ()
    {
        int item=first.item;
        first=first.next;
        return item;
    }
}

import javax.swing.*;

public class main {

    public static void main(String[] args) {
        Stack ob=null;
        int num=0;
        while (true)
        {
            num=Integer.parseInt(JOptionPane.showInputDialog("Enter the number"));
            ob.push(num);
            if (num==0)
                break;
        }
        int k;
        k=ob.pop();
        JOptionPane.showMessageDialog(null, k);     
    }

now when I enter a number the compiler through an Execption java.lang.NullPointerException at main.main(main.java:18)

Why this is happening and how to avoid it Please be patient and thanks in advance

Fritz
  • 9,987
  • 4
  • 30
  • 49
Coderji
  • 7,655
  • 5
  • 37
  • 51

3 Answers3

5

Your stack ob is null when you call push. You need to instantiate it. Instead of

Stack ob=null;

you need to have

Stack ob = new Stack();
thegrinner
  • 11,546
  • 5
  • 41
  • 64
  • Wow!! i didn't know that if you don't have contractor you can initialize it like this!!! – Coderji Feb 20 '13 at 14:38
  • @OsamaEspil If you don't define a constructor Java does it for you. See this [answer](http://stackoverflow.com/a/4488766/264775) for more detail. – thegrinner Feb 20 '13 at 14:45
4

Your code:

Stack ob=null
...
ob.push(num);

ob is null, you never assign an object to it. Calling a method on null will always result in a NullPointerException.

You can fix it like this:

Stack ob = new Stack();
jlordo
  • 37,490
  • 6
  • 58
  • 83
0

Most exceptions have a self-explanatory name. This one, for example, is a NullPointerException - there is a null object that is being accessed and the exception is being called.

You are trying to define "ob" as a new Stack object, although you have simply defined it as null. Between the definition and the method call (ob.push()), ob is not modified again and therefore it remains null, causing the exception.

To fix this, you need to ensure that the object is not null when you call it. There are two ways to fix the error.

Firstly, you could instantiate the object. This would make the program work the way that you want it to. To do this, simply define the object like so: Stack ob = new Stack(); rather than Stack ob = null;.

The second way to fix this would be to catch the error. This would make it so that the program continues if an error occurs. I recommend that you use this whenever there is a potential that an object could be null, so that the program doesn't just shut down. To do this, surround the line with a try-catch-clause:

try
{
    ob.push(num);
}
catch (NullPointerException e)
{
    System.out.println("Error at: ob.push(num);");
}
James Monger
  • 10,181
  • 7
  • 62
  • 98