0

I am trying to create a function to delete an element from a linked list. However, I keep getting a NullPointerException. When I run the program, it states:

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

How can I delete an element from the linked list without having this exception thrown? Here is the code

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;
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
evarias
  • 23
  • 1
  • 2
  • 6
  • Do you prefer this question to be closed as duplicate of [null ref exception in Java](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) or [correct implementation of linked list in Java](http://stackoverflow.com/questions/10042/how-do-i-implement-a-linked-list-in-java)? – Alexei Levenkov Nov 30 '14 at 21:33

1 Answers1

0

In add method you work with 'list', in delete method you work with 'head'. Retain the only property and work with it:

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;
            }
        }
    }
}
ursa
  • 4,404
  • 1
  • 24
  • 38
  • got it, but the program is still giving the NullPointerException at the 'if (list.data.equals(x))' line. how can I fix this? – evarias Nov 30 '14 at 21:55
  • take a look on ElephantNode constructor - what do you actually save and what do you check in delete method – ursa Nov 30 '14 at 21:56
  • I am going to assume I save and check the Object data? – evarias Nov 30 '14 at 22:09
  • Debug your program in IDE e.g. in [Eclipse](http://www.youtube.com/watch?v=QnOCXd5o6E4). It will help you to understand what actually happens with your program – ursa Nov 30 '14 at 22:16
  • When I debug it, it says no details to display, – evarias Nov 30 '14 at 22:37