1

so I am having a few errors in my code but Im not sure what they are telling me to change. This is my first linked list code. If anyone can help me out i would appreciate it.

This is my linked list

  public class MyLinkedList<E> 
 {
private Node<E> head = null;

public void add(E element)
{
    if(size() == 0)
    {
        head = new Node<E>(element);
        return;
    }

    Node<E> cursor = head;

    while (cursor.next != null)
    {
        cursor = cursor.next;
    }

    cursor.next = new Node<E>(element);
}


public void add(int index, E element)
{
    Node<E> cursor = head;
    E temp, before;

    for(int x = 0; x < index; x++)
    {
        cursor = cursor.next;
    }

    before = cursor.content; 
    cursor.content = element;

    while(cursor.next != null)
    {
        cursor = cursor.next;
        temp = cursor.content;
        cursor.content = before;
        before = temp;
    }

    add(before);
}

public boolean remove(E element)
{
    Node<E> cursor = head;

    if (head.content == element)
    {
        head = cursor.next;
        return true;
    }

    while(cursor.next != null)
    {
        if (cursor.next.content == element)
        {
            cursor.next = cursor.next.next;
        }
        else
        {
            cursor = cursor.next;
        }

    }

    if (cursor.next == null)
    {
        return false;
    }

    return true;
}

public E remove(int index)
{
    E result = null;

    if (index < 0 || index >= size())
    {
        return null;
    }

    Node<E> cursor = head;

    for (int x = 0; x < index; x++)
    {
        cursor = cursor.next;
    }

    result = cursor.content;

    cursor = head;
    for (int x = 0; x < index - 1; x++)
    {
        cursor = cursor.next;
    }

    if(index != 0)
    {
        cursor.next = cursor.next.next;
    }
    else
    {
        head = cursor.next;
    }
    return result;
}

public E set(int index, E element)
{
    Node<E> cursor = head;
    E temp;
    for (int x = 0; x < index; x++)
    {
        cursor = cursor.next;
    }

    temp = cursor.content;
    cursor.content = element;
    return temp;
}

public boolean contains(E element)
{
    Node<E> cursor = head;

    while(cursor != null)
    {
        if(cursor.content == element)
        {
            return true;
        }
        cursor = cursor.next;
    }

    return false;
}

public E get(int index)
{
    Node<E> cursor = head;
    if (index < 0 || index >= size())
    {
        return null;
    }

    for (int x = 0; x < index; x++)
    {
        cursor = cursor.next;
    }

    return cursor.content;
}

public int indexOf(E element)
{
    Node<E> cursor = head;
    int index = 0;

    while (cursor != null)
    {
        if(cursor.content == element)
        {
            return index;
        }

        index++;
        cursor = cursor.next;
    }

    return -1;
}

public boolean isEmpty()
{
    if (size() == 0)
    {
        return true;
    }
    return false;
}

public int size()
{
    Node<E> cursor = head;
    int count = 0;

    while (cursor != null)
    {
        count++;
        cursor = cursor.next;
    }
    return count;
}

public void dumpList()
{
    Node<E> cursor = head;

    while (cursor != null)
    {
        System.out.println(cursor.content);
        cursor = cursor.next;
    }
}
 }

This is my node code

  public class Node<E>
  { 
  public E content;
public Node<E> next;

public Node(E content)
{
    this.content = content;
}

public Node(E content, Node<E> next)
{
    this(content);
    this.next = next;
}

public String toString()
{
    return content.toString();
}
   }

and this is the code we are testing it with

  public class Demo4
  {
public static void main(String[] args)
{
    MyLinkedList<String> t = new MyLinkedList<String>();

    t.add("Santa Maria");
    t.add("Los Angeles");
    t.add("Ventura");
    t.add("Thousand Oaks");
    t.add(0, "Orcutt");
    t.add(5, "Pismo");
    t.add(3, "San Luis Obispo");
    t.set(1, "London");
    t.set(0, "San Diego");
    t.set(6, "Tokyo");
    t.add("Westlake");

    t.remove("Santa Maria");
    System.out.println("was Tokyo found? " + t.remove("Tokyo"));
    t.remove("Westlake");
    System.out.println("was Dubai found? " + t.remove("Dubai"));
    t.remove("Pismo");

    System.out.println("Remove index 5. It contained: " + t.remove(5));
    System.out.println("Remove index 0. It contained: " + t.remove(0));
    System.out.println("Remove index 2. It contained: " + t.remove(2));
    System.out.println("Here's what's left over");
    for (int x = 0; x < t.size(); x++)
    {
        System.out.println(t.get(x));
    }

    System.out.println("--------");
    System.out.println("Cool!  I didn't crash!");
}
   }


 my error in eclipse is the following
Exception in thread "main" java.lang.NullPointerException
at MyLinkedList.add(MyLinkedList.java:35)
at MyLinkedListDemo.main(MyLinkedListDemo.java:12)
AstroCB
  • 12,337
  • 20
  • 57
  • 73

2 Answers2

0

See, Eclipse is telling you everything a human can tell you :)

this is your error:

Exception in thread "main" java.lang.NullPointerException
at MyLinkedList.add(MyLinkedList.java:35)
at MyLinkedListDemo.main(MyLinkedListDemo.java:12)

Eclipse says "there is a Null Pointer Exception" in line no 35 of the file MyLinkedList.java, when the add() method is called. This was actually invoked my the main() of MyLinkedListDemo.java in line no 12.

Now put a debug point on that line, and you see what is null and why it is null. You get a NPE, when you try to invoke something on null

spiderman
  • 10,892
  • 12
  • 50
  • 84
0

You are getting a null pointer exception because you are trying to insert value to a position that doesn't exist in the memory. Remove the lines that add to index 5 and 6 and your code will work.

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

    MyLinkedList<String> t = new MyLinkedList<String>();

    t.add("Santa Maria");
    t.add("Los Angeles");
    t.add("Ventura");
    t.add("Thousand Oaks");
    t.add(0, "Orcutt");
    t.add(3, "San Luis Obispo");
    t.set(1, "London");
    t.set(0, "San Diego");
    t.add("Westlake");

    t.remove("Santa Maria");
    System.out.println("was Tokyo found? " + t.remove("Tokyo"));
    t.remove("Westlake");
    System.out.println("was Dubai found? " + t.remove("Dubai"));
    t.remove("Pismo");

    System.out.println("Remove index 5. It contained: " + t.remove(5));
    System.out.println("Remove index 0. It contained: " + t.remove(0));
    System.out.println("Remove index 2. It contained: " + t.remove(2));
    System.out.println("Here's what's left over");
    for (int x = 0; x < t.size(); x++) {
        System.out.println(t.get(x));
    }

    System.out.println("--------");
    System.out.println("Cool!  I didn't crash!");
}

}

DanielAnenia
  • 106
  • 2
  • 5