在单链接列表的最后一个节点中获取nullpointer异常

时间:2015-06-10 13:50:58

标签: java nullpointerexception linked-list

public class Node {
    public String name;
    public Node next;

    public Node(String name, Node next  ){
        this.name = name;
        this.next = next;

    }
    public String getName(){
        return name;
    }
    public void setName(String n){
        name = n;
    }
    public Node getNext(){
        return next;
    }
    public void setNext(Node n){
        next = n;
    }
    public String toString() {

       return "Name " + name;
    }
}

public class LinkedList {

    Node head = null;

    int nodeCount= 0;

    int counter = 0;

    LinkedList(){
         head = null;

    }
   public boolean isEmpty(){
       return  (head != null);
   }

    public void insertNode( String name ){

        if( head == null){
            head = new Node(name, null);
            nodeCount++;
        }else{
            Node temp = new Node(name, null);
            temp.next = head;
            head = temp;


            nodeCount++;
        }


    }
    public Node reverseTest(Node L){



          if(L == null || L.next ==null){
              return L;
          }

          Node remainingNode =  reverseTest(L.next);
          Node cur = remainingNode;
          while(cur.next !=null){
              cur=cur.next;
          }

          L.next = null;
          cur.next = L;

         return  remainingNode;

    }

    public boolean searchLinkedList(Node L, String S){
        if (L == null)
            return false;
        else{
            while(L !=null){
                if(S.equals(L.name))
                    return true;
                L= L.next;
            }
        }

        return false;
    }

    public String toString(){


        Node current = head;
        String output = "";
        while(current !=null){
            output += "[" + current.getName() + "]";

            current = current.getNext();
        }

        return  output;

    }


}

public class LinkedListDemo {

    public static void main(String[] args){

        LinkedList FriendList = new LinkedList();

        FriendList.insertNode("First");
        FriendList.insertNode("Second");
        FriendList.insertNode("Third");
        FriendList.insertNode("Fourth");


       FriendList.searchLinkedList(FriendList.head, "Hello");

        String NameList = FriendList.toString();
        System.out.println(NameList);
        System.out.println("Finish");

    }
}

NullPointerException

我有一个单独的链表。我想搜索链表中和最后一个循环中不存在的值,当它达到L = L.next我收到NPE。我不明白这里有什么错误。请指出正确的方向。

1 个答案:

答案 0 :(得分:0)

正如我在评论中指出的那样,没有错误。您的IDE只是向您显示这些属性的最后读数。

当你到达最后一个循环时:

public boolean searchLinkedList(Node L, String S){
    if (L == null)
        return false;
    else{
        while(L !=null){
            if(S.equals(L.name))
                return true;
            L= L.next;
        }
    }

    return false;
}

L确实等于null;这意味着当您的IDE尝试读取其属性(名称和下一个)时,它(并且只有IT,而不是您的程序的实际输出)才会获得合理的NullPointerException。

该程序运行正常。