Java 在我的单链表实现中遇到NullPointerException

Java 在我的单链表实现中遇到NullPointerException,java,nullpointerexception,singly-linked-list,Java,Nullpointerexception,Singly Linked List,因此,我正在研究一个单链表的实现,我遇到了可怕的空指针异常。这是我的密码: class Link { public Object data; public Link next; } class List { private Link _head; private Link _tail; private int counter = 0; public List() { _head = null; _tail = null;

因此,我正在研究一个单链表的实现,我遇到了可怕的空指针异常。这是我的密码:

class Link
{
   public Object data;
   public Link next;
}

class List
{
   private Link _head;
   private Link _tail;
   private int counter = 0;

   public List()
   {
      _head = null;
      _tail = null;
   }

   public void insert(Object item, int pos)
   {
      if (pos == counter)
      {
         insertAtEnd(item);
         return;
      }

      Link temp = _head;
      for (int i = 1; i < pos; ++i)
      {
         temp = temp.next;
      }

      Link added = new Link();
      added.data = item;
      added.next = temp.next;
      temp.next = added;
      ++counter;
   }

   public void insertAtEnd(Object item)
   {
      if (_head == null)
      {
         Link added = new Link();
         added.data = item;
         added.next = _tail;
         _head = added;
      }
      else
      {
         _tail.next = new Link();
         _tail = _tail.next;
         _tail.data = item;
      }
      ++counter;

   }

   public void deleteRange(int start, int end)
   {
      Link temp = _head;
      Object deleted;

      for (int i = 1; i < start - 1; ++i)
      {
         temp = temp.next;
      }
      for (int i = start; i < end; ++i)
      {
         deleted = temp.next.data;
         temp = temp.next;
         --counter;
      }
      temp.next = temp.next.next;
   }

   public void deleteAll(Object item)
   {
      Link temp = _head;
      Link temp2 = _head;
      Link prev = null;
      Object deleted;
      int times = 0;

      for (int i = 1; i <= counter; ++i)
      {
         if (temp.data.equals(item))
            ++times;
         temp = temp.next;      
      }

      for (int i = 1; i <= counter; ++i)
      {
         prev = temp2;
         if (temp2.data.equals(item))
         {
            deleted = temp2.data;
            temp2 = prev;
         }
         temp2 = temp2.next;

      }
      counter = counter - times;
   }

   public Object retrieve(int pos)
   {
      Link temp = _head;

      for (int i = 1; i < pos; ++i)
      {
         temp = temp.next;
      }
      return temp.data;
   }

   public List find(Object item)
   {
      Link temp = _head;
      Link prev = null;
      int count = 0;
      boolean found = false;
      List positions = new List();
      positions.insertAtEnd((Integer) count);
      return positions;

   }

   public int getSize()
   {
      return counter;
   }

   public String toString()
   {
      Link temp = _head;
      String s = "";
      while (temp.next != null)
      {
         s += temp.data + ", ";
         temp = temp.next;
      }   
      return s;   
   }
类链接
{
公共对象数据;
公共链接下一步;
}
班级名单
{
专用链路头;
专用链路;
专用整数计数器=0;
公开名单()
{
_head=null;
_tail=null;
}
公共作废插入(对象项,内部位置)
{
如果(位置==计数器)
{
插页(项目);
返回;
}
链路温度=_头;
对于(int i=1;i对于(inti=1;i我认为,在函数InsertAtEnd()中),您没有在第一个if条件下初始化_head。

您能捕获异常并将堆栈跟踪添加到您的问题中吗?您可以检查
temp.next
是否为null,但请注意
temp
也可以为null,因为
_head
被初始化为null。
_head
的值是多少到列表中了吗?编辑前面的注释:列表的
toString()
应该简单地将
temp.toString()
添加到列表中,而
链接
对象应该有一个
toString()
来隔离打印的内容--
数据
字段。@JonK你能进一步研究吗?