首页 > 解决方案 > 链表的 getlast() 中的 NullPointerException

问题描述

我的链表有这个问题,当我尝试访问最后一个节点数据部分时,它会抛出一个空异常

代码部分:getlast() 方法与主类中的对象

package lab1_ds;
    public class LAB1_DS {
        public static void main(String[] args) {
               singlylinkedlist mylist=new singlylinkedlist();
            singlylinkedlist<person> plist=new singlylinkedlist();
            plist.addfirst(new person("Hesssa","SA"));
            plist.addfirst(new person("Nora","SA"));
            plist.display();
            plist.addnode(new person("Farah","SA"),2);
             plist.display();
             System.out.println(plist.first().getName());
             plist.removeNode(plist.getlast());
             plist.display();
        }

    }

链表代码为:

package lab1_ds;
public class singlylinkedlist <E>{
    private static class Node<E>{
        private E element;
        private Node<E> next;

        public Node(E element, Node<E> next) {
            this.element = element;
            this.next = next;
        }

        public E getElement() {
            return element;
        }

        public Node<E> getNext() {
            return next;
        }

        public void setNext(Node<E> next) {
            this.next = next;
        }

    }
    private Node<E> head=null;
    private Node<E> tail=null;
    private int size=0;

    public singlylinkedlist() {
    }
    public void display(){
         Node<E> current;
         current=head;
         int count=0;
         System.out.println("\n-----------Display method-----------");
         while(current!=null){
             count++;
              System.out.println("Linked list ("+count+"):"+current.getElement());
              current=current.getNext();

         }
    }
    public int size(){
        return size;
    }
    public boolean isEmpty(){
        return size==0;
    }
    public E getlast(){
        if (isEmpty())  
            return null;
        return tail.getElement(); 
    }




    public void setTail(Node<E> tail) {
        this.tail = tail;
    }

     public E first(){
        if (isEmpty())
            return null;
        return head.getElement();      
    }
    public void addfirst(E value){
         Node<E> newNode= new Node(value,null);
         newNode.next=head;
         head=newNode;
         size=size+1;
         if(size==1)
             head=tail;
    }
    public void addlast(E value){
        Node<E> newNode= new Node(value,null);
        if(size==0)
            head=tail=newNode;
        else{  tail.next=newNode;
        tail=newNode;
        }
        size=size+1;
    }
    public void addnode(E value,int pos){
        Node<E> current;
     if(pos==1)
         addfirst(value);
      if(pos==size+1)
         addlast(value);
       Node<E> newNode= new Node(value,null);
       current=head;
       int count=1;
    while(count<pos-1 && current.next!=null){
        current=current.next;
        count=count+1;
    }
    newNode.next=current.next;
    current.next=newNode;
    size++;//or size=size+1;

    }
     public void removeFirst(){
         if (isEmpty()){
             System.out.println("linked list is empty");
             return;
         }
         head=head.getNext();
         size--;//or size=size-1;
         if(size==0)
             tail=null;//only node

     }
      public void findNode(E place){
           if (isEmpty()){
             System.out.println("linked list is empty");
             return;
         }
          Node<E> current=head;
          int count=1;
          while (current!=null ){
              if(current.getElement()==place || current.getElement().equals(place)){
                   System.out.println("found in posittin #"+count);
              return;}
               count++;
          current=current.getNext();
          }//end while loop

          System.out.println("\n.......Node is not found!......");
      }
      public void removeNode(E place){
          Node<E> current=head;
         Node<E> prev=head;
            if (isEmpty()){
             System.out.println("linked list is empty");
             return;
         }
            while(current.getElement()!=place && !current.getElement().equals(place)){
                if(current.next==null){
                    System.out.println("\n not found...");
                    return;
                }
                prev=current;
                current=current.next;
            }//end loop
            if(current==head){
                removeFirst();
            }
            else {
                prev.next=current.next;
                size--;
            }
            if(current==tail){//node i'm trying to remove is the last node 
                tail=prev;
            }
      }
}

错误显示:

Exception in thread "main" java.lang.NullPointerException
at lab1_ds.singlylinkedlist.getlast(singlylinkedlist.java:52)
    at lab1_ds.LAB1_DS.main(LAB1_DS.java:34)

第 52 行:

  return tail.getElement(); 

第 34 行:

plist.removeNode(plist.getlast());

请帮助我,我似乎无法弄清楚,实验室助理说它应该可以工作(getlast()

标签: javadata-structureslinked-list

解决方案


问题出在您的 addfirst 方法中。您将头重新分配给空,而不是尾对头。正确版本:

public void addfirst(E value) {
        Node<E> newNode = new Node(value, null);
        newNode.next = head;
        head = newNode;
        size = size + 1;
        if (size == 1)
            tail = head;
    }

更改后,输出为:

-----------Display method-----------
Linked list (1):person{name='Hesssa', state='SA'}

-----------Display method-----------
Linked list (1):person{name='Nora', state='SA'}
Linked list (2):person{name='Hesssa', state='SA'}

-----------Display method-----------
Linked list (1):person{name='Nora', state='SA'}
Linked list (2):person{name='Farah', state='SA'}
Linked list (3):person{name='Hesssa', state='SA'}
Nora

-----------Display method-----------
Linked list (1):person{name='Nora', state='SA'}
Linked list (2):person{name='Farah', state='SA'}

旁注

请参阅类和方法的命名约定。它们应该装在蛇盒里。例如,来自addfirst->的方法名称addFirst。类名来自singlylinkedlist->SinglyLinkedList


推荐阅读