我是java新手,我正在尝试编写一个链接列表堆栈..
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);
}
现在,当我通过 main.main(main.java:18) 处的 Execption java.lang.NullPointerException 输入数字时
为什么会发生这种情况以及如何避免它请耐心等待并提前致谢