0

I just want to make sure what I am receiving from an Eclipse is an error, or it's fine? Here is my code:

package prj2;

public class Sandbox {
    public static void main(String[] args) {
        NodeChain<Integer> myList = new NodeChain<Integer>();

        myList.add(1); 
        myList.add(2); 
        myList.add(0, -99); 
        myList.add(3, 45);
        myList.add(99);
        myList.add(4, 50);

        myList.remove(0);
        myList.remove(3);
        myList.remove(1);

        System.out.println(myList.contains(45));

        myList.print();
        System.out.println("myList has size "+myList.size());
    }

}

And here is the result:

Exception in thread "main" java.lang.NullPointerException
    at prj2.NodeChain.contains(NodeChain.java:57)
    at prj2.Sandbox.main(Sandbox.java:18)

If that's an error can you please help for a fix?

Source for NodeChain:

package prj2;

public class NodeChain<E> implements ListADT<E>{
    private ListNode<E> head; // this is a dummy header 
    private ListNode<E> tail;
    private int numItems;

    public NodeChain() {
        head = new ListNode<E>(null);
        tail = head;
        numItems = 0;
    }

    public boolean isEmpty() { return numItems == 0; }

    public void add(E item) { 
        // Note the lack of a need for null checks
        tail.setNext(new ListNode<E>(item, tail.getNext()));
        tail = tail.getNext();
        numItems++;
    }

    public void add(int pos, E item) {
        // Handle the cases where an invalid pos is given
        if (pos < 0 || pos > numItems) throw new IndexOutOfBoundsException();

        // Handle the case where we're adding to the end of the list.
        if (pos == numItems) {
            add(item);
            return;
        }

        // No other special case handling required
        // Traverse the list until we get to the point of insertion and execute the insertion.
        ListNode<E> tmp = traverseTo(pos);
        tmp.setNext(new ListNode<E>(item, tmp.getNext()));

        // Increment numItems
        numItems++;
    }

    public int size() { return numItems; }

    public E remove(int pos) { // Left unimplemented
        return null;
    }

    public E get(int pos) {
        if (pos < 0 || pos >= numItems) throw new IndexOutOfBoundsException();
        ListNode<E> node = traverseTo(pos+1);
        return node.getData();
    }

    public boolean contains(E obj) {
        ListNode<E> tmp = head;
        while (tmp != null) {
            if (tmp.getData().equals(obj)) return true;
            tmp = tmp.getNext();
        }
        return false;
    }

    private ListNode<E> traverseTo(int pos) {
        if (pos < 0 || pos >= numItems) throw new IndexOutOfBoundsException();
        ListNode<E> tmp = head;
        for (int i = 0; i < pos; i++) 
            tmp = tmp.getNext();
        return tmp;
    }

    /* Extra method to facilitate debugging */
    public void print() {
        ListNode<E> tmp = head;
        while (tmp != null) {
            System.out.print(tmp.getData()+", ");
            tmp = tmp.getNext();
        }
        System.out.println();
    }
}

Source for ListNode:

package prj2;

public class ListNode<T> {
    private T data;
    private ListNode<T> next;

    public ListNode(T obj) {
        this(obj, null);
    }

    public ListNode(T obj, ListNode<T> ptr) {
        data = obj;
        next = ptr;
    }

    public void setData(T obj) { data = obj; }

    public T getData() { return data; }

    public void setNext(ListNode<T> n) { next = n; }

    public ListNode<T> getNext() { return next; }
}

Source for ListADT:

package prj2;

/**
 * A List is an ordered collection of items.
 */
public interface ListADT<E> {
    /**
     * Add item to the end of the List.
     *
     * @param item the item to add
     */
    void add(E item);

    /**
     * Add item at position pos in the List, moving the items
     * originally in positions pos through size()- 1 one place
     * to the right to make room.
     *
     * @param pos the position at which to add the item
     * @param item the item to add
     * @throws IndexOutOfBoundsException if pos is less than 0
     * or greater than size()
     */
    void add(int pos, E item);

    /**
     * Return true iff item is
     * item x in the List such
     *
     * @param item the item to
     * @return true if item is
     */
    boolean contains(E item);

    /**
     * Return the number of items in the List.
     *
     * @return the number of items in the List
     */
    int size();

    /**
     * Return true iff the List is empty.
     *
     * @return true if the List is empty, false otherwise
     */
    boolean isEmpty();

    /**
     * Return the item at position pos in the List.
     *
     * @param pos the position of the item to return
     * @return the item at position pos
     * @throws IndexOutOfBoundsException if pos is less than 0
     * or greater than or equal to size()
     */
    E get(int pos);

    /**
     * Remove and return the item at position pos in the List,
     * moving the items originally in positions pos+1 through
     * size() one place to the left to fill in the gap.
     *
     * @param pos the position at which to remove the item
     * @return the item at position pos
     * @throws IndexOutOfBoundsException if pos is less than 0
     * or greater than or equal to size()
     */
    E remove(int pos);
}
Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
  • 5
    It's an error and without the source for `NodeChain`, it's impossible to know why – MadProgrammer Jul 03 '14 at 05:11
  • 1
    The stacktrace seems to provide all the info you need. – Michael Sanchez Jul 03 '14 at 05:12
  • 3
    I can't imagine many times where a `NullPointerException` + stack trace + program exit is the *correct* output for a program... – awksp Jul 03 '14 at 05:12
  • @MadProgrammer I added the rest of the code in the updated question. Can you please take a look? – Mona Jalal Jul 03 '14 at 05:45
  • @MadProgrammer What's wrong with this code? I don't quite get it? `public boolean contains(E obj) { ListNode tmp = head; while (tmp != null) { if (tmp.getData().equals(obj)) return true; tmp = tmp.getNext(); } return false; }` – Mona Jalal Jul 03 '14 at 06:14
  • This is going to be hard to explain, but, you create a `head` using `new ListNode(null);`, which means when you call `getData` on it, it will return `null`. You never reset this. Instead, when you add a new element, you need to determine if there is anything already in your `NodeChain`, if there is not, you need make the node the head – MadProgrammer Jul 03 '14 at 06:17
  • This means that in your constructor, you should not be creating a `head` or `tail`, they should remain `null`. When `add` is called, you need to check if `head` is `null`, if it is, you create a `new ListNode`, assign it to head and tail and the continue with the rest of the add process. If `head` is not `null`, you simply need to create a new instance of the `ListNode` and proceed as normal... – MadProgrammer Jul 03 '14 at 06:21
  • 1
    I'm sorry, this is going to be messy, but you're using an IDE with auto formatting, it should just about fix it... `public void add(E item) { // Note the lack of a need for null checks ListNode next = null; if (head == null) { head = new ListNode(item); tail = head; next = head; } else { next = new ListNode(item, tail.getNext()); } tail.setNext(next); tail = tail.getNext(); numItems++; }` – MadProgrammer Jul 03 '14 at 06:22
  • @MadProgrammer got you. This code of mine fixed it thanks to your hint `public void add(E item) { // Note the lack of a need for null checks if (numItems==0){ head = new ListNode(item,null); tail=head; numItems++; } else{ tail.setNext(new ListNode(item, tail.getNext())); tail = tail.getNext(); numItems++; } }` – Mona Jalal Jul 03 '14 at 06:25

1 Answers1

1

Well,it is clearly reflected from the output that it's an Exception thrown or rather Error.The exception thrown is java.lang.NullPointerException!

Also,the stack trace of what the error is about is printed below the Exception thrown line:-

at prj2.NodeChain.contains(NodeChain.java:57)
at prj2.Sandbox.main(Sandbox.java:18)

Also,it's clearly visible from your stack trace that there is an exception being thrown at System.out.println(myList.contains(45));.

You better resolve this error after checking your code of contains() method in NodeClass Class!

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73