0

我正在用Java编写一个堆栈程序。在代码中,推送函数导致空指针异常。我猜该节点没有被创建。请指教。提前致谢

//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    

-------------我得到的输出如下-----------------------------

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)
4

4 回答 4

1

在您的代码中:

public class Stack_using_ll{
    private Node first;

private Node first仅声明对名为“first”的节点的引用,它不会创建节点的实例供其引用。

因此,当您稍后分配first给 时oldFirst,您分配的是null,尝试访问 null 的成员会导致 NPE

public void push(String item){
        Node old_first = first;
        first = new Node();
        first.str = item;
        first.next = old_first.next; <- NPE here
于 2013-10-14T13:27:06.600 回答
0

first开头是 null 所以当你这样做时:old_first = firstold_first 变为 null,所以old_first.next给你exception

解决方案:

 public void push(String item){
        Node old_first = first;
        first = new Node();
        first.str = item;
        if(old_first!=null)
           first.next = old_first.next;
    ...}
于 2013-10-14T13:24:05.187 回答
0

在您的 main 方法中,您创建一个新的 Stack_using_ll 对象,这会创建任何成员变量,但是您永远不会给出first值(例如在构造函数中),因此它保持为空。

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

然后,您调用stack.push("Jeans");您的 main 方法,该方法尝试使用first,但first为 null,因此出现异常。

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++;
}
于 2013-10-14T13:27:10.840 回答
0

问题是您第一次向对象推送时的空指针(因为第一项为空,而您尝试获取它的 .next

请参阅下面的更正:(在推送功能中)

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++;
}
于 2013-10-14T13:27:49.647 回答