带链接列表的NullPointerException(删除元素)

时间:2014-11-30 21:30:14

标签: java exception null linked-list

我正在尝试创建一个从链表中删除元素的函数。但是,我一直得到一个NullPointerException。当我运行程序时,它声明:

Exception in thread "main" java.lang.NullPointerException
    at ElephantList.delete(ElephantList.java:30)
    at BabyElephantWalk.main(BabyElephantWalk.java:15)

如何在不抛出此异常的情况下从链接列表中删除元素?这是代码

BabyElephantWalk.java

public class BabyElephantWalk
{
    public static void main (String[] args)
    {

        ElephantList walk = new ElephantList();
        walk.add (new Elephant("Charlie"));
        walk.add (new Elephant("Donna"));
        walk.add (new Elephant("Chilli"));
        walk.add (new Elephant("Piper"));
        walk.add (new Elephant("Ziva"));

        walk.delete ("Charlie");
        System.out.println (walk);
    }
}

ElephantList.java

public class ElephantList
{
private ElephantNode head;
private ElephantNode list;
public ElephantList()
{
    list = null;
}//end ElephantList constructor

public void add(Elephant cat)
{
    ElephantNode node = new ElephantNode(cat);
    ElephantNode current;
    if (list == null)
        list = node;
    else
    {
        current = list;
        while (current.next != null)
            current = current.next;
        current.next = node;
    }
}//end add

public void delete(Object x)
{
    {
        if (head.data == x)
        {
            head = head.next;
        }
        else
        {
            ElephantNode temp = head;
            while (temp.next != null)
            {
                if (temp.next.data.equals(x))
                {
                    temp.next = temp.next.next;
                    break;
                }
                else 
                {
                    temp = temp.next;
                }
            }
        }
    }
}

public String toString ()
{
    String result = "";
    ElephantNode current = list;
    while (current != null)
    {
        result += current.elephant + "\n";
        current = current.next;
    }
    return result;
}//end toString

private class ElephantNode
{
    public Elephant elephant;
    public ElephantNode next;
    public Object data;
    public ElephantNode(Elephant cat)
    {
        elephant = cat;
        next = null;
        data = null;
    }//end ElephantNode constructor
}//ElephantNode
}//end ElephantList

Elephant.java

public class Elephant
{
    private String title;
    public Elephant(String newname)
    {
        title = newname;
    }//end Elephant constructor
    public String toString ()
    {
        return title;
    }
}

1 个答案:

答案 0 :(得分:0)

在添加方法中,您使用' list',在删除方法中,您使用' head'。保留唯一的财产并使用它:

public void delete(Object x)
{
    if (list.data.equals(x))   // <<= 'head' replaced with 'list'; == replaced with .equals
    {
        list = list.next; // <<= 'head' replaced with 'list'
    }
    else
    {
        ElephantNode temp = list; // <<= 'head' replaced with 'list'
        while (temp.next != null)
        {
            if (temp.next.data.equals(x))
            {
                temp.next = temp.next.next;
                break;
            }
            else 
            {
                temp = temp.next;
            }
        }
    }
}