如何在链表中正确编写显示列表方法?下面显示的是我的方法,但它给出了一个空指针异常



这是我的代码。 请告诉我这里出了什么问题以及如何纠正它

   public void displayList(){                   //displays items on the list
   Node current = head;
   while(current!=null){
       current = current.next;
       System.out.println(current);
   }

对于初学者来说,在 Node current = head; 中,除非它是预定义的字段,否则head不存在。

字段有时不是偶然初始化的。当心这一点。

最新更新