为什么我的代码中出现空指针异常错误?

时间:2018-11-10 19:21:56

标签: java linked-list

为什么在prev.value.compareTo(v)<0if(cur.value.compareTo(v)<0处出现空指针异常错误?

public void insert (T v) {
        Node prev = head, cur = head.next;


        if(prev==null)
            {
            prev=head;

            }

        else if(prev.value.compareTo(v)<0)  //v belongs in the front
        {
            prev.value=v;
            prev=prev.next;
        }

        else
        {

            //iterate through the list
            prev=prev.next;
            cur=cur.next;
            while(cur!=null)
            {
                if(cur.value.compareTo(v)<0) 
                {
                    prev=cur;
                    cur=cur.next;
                }

            }

        }
}

0 个答案:

没有答案