Java Linked List实现(来自AbstractList)在Collections.sort上给出nullPointerException

时间:2013-11-23 19:28:59

标签: java linked-list nodes abstract

我是Java和OOP的新手,我以前学过的语言是C. 我正在尝试创建一个扩展AbstractList的链接列表,并允许使用Collections.sort()函数。问题是,当我调用Collections.sort()函数时,我得到一个nullPointerException。我的猜测是异常是因为我列表中的最后一个节点是空节点(因此我可以知道列表的结束位置)。

class Node
{
    Object o;
    Node next;
    public Node(Object n)
    {
        o = n;
        next = null;
    }
}

class LinkList extends AbstractList
{
    Comparator c;
    public Node head, last;
    public LinkList(Comparator c)
    {
        this.c = c;
        head = null;
        last = null;
    }

    @Override
    public boolean add(Object a)
    {
        Node t = new Node(a);
        if(last == null)
        {
            head = t;
            last = t;
            last.next = null;
        }
        else //thanks, hyde
        if(last != null)
        {
            last.next = t;
            last = t;
            last.next = null;

        }
        return true;
    }
    @Override
    public Object get(int a)
    {
        Node it = head;
        int contor = 0;
        while(it!=null && contor<a)
        {
            it = it.next;
        }

        if(it!=null)
        {
            return it;
        }
        else
            return null;
     }

@Override
public Object set(int i, Object a)
{
    Node it = head;
    int contor = 0;
    Node aux;
    while(it!=null && contor<i)
    {
        it = it.next;

    }
     if(it!=null)
     {
        aux = it;
        it.o = a;
       // Collections.sort(this,c);
        return aux;
     }
     else
        return null;
}

@Override
public int size()
{
    Node it = head;
    int contor = 0;
    while(it!=null)
    {
        contor++;
        it = it.next;
    }
    return contor;
}

@Override
public int indexOf(Object a)
{
    Node it = head;
    int contor = 0;
    while(it!=null && it.o.equals(a)==false)
    {
        it = it.next;
        contor++;
    }
    if(it!=null)
    {
        return contor;
    }
    else
        return -1;
}

}

public class Test
{
    public static void main(String args[])
    {
        LinkList lista = new LinkList(new Comparator(){
        @Override
        public int compare(Object o1, Object o2)
        {
             int s1 = (int) o1;
             int s2 = (int) o2;
             return s2-s1;
        }
        });
        lista.add(2);
        lista.add(3);
        Collections.sort(lista); //this is line 156
        System.out.println(lista.size());
    }
}

基本上,我添加了两个元素,我尝试对列表进行排序,然后得到nullPointerException。感觉非常令人沮丧,因为我无法控制排序功能。

Exception in thread "main" java.lang.NullPointerException
at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:157)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
at java.util.Arrays.sort(Arrays.java:472)
at java.util.Collections.sort(Collections.java:155)
at Ex6.main(Ex6.java:156)
Java Result: 1

1 个答案:

答案 0 :(得分:1)

contor++方法中的循环中缺少get,因此它始终返回null