2

I cant figure out where I'm going wrong at all, I've checked and rechecked my add(int index, T obj) method countless times, still getting the same error. Here's my code, any pointers would be GREATLY appreciated; this problem has been holding up my project for a day or so at least now.

package edu.neumont.csc250;


class LinkedList<T> implements List<T>{

    Node<T> head;
    Node<T> tail;
    int listCount;

    public LinkedList(){
        head = null;
        listCount = 0;
    }

    @Override
    public T get(int index) throws IllegalArgumentException {
        if(index > size() - 1 || index < 0){
            throw new IllegalArgumentException();
        }
        else{           
            Node<T> current = head;
            for(int i = 0; i < index; i++)
            {               
                current = current.next;
            }
            if(current.content != null){
                return current.content;
            }
            else{
                System.out.println("Null value.");
                return null;
            }
        }
    }

    @Override
    public void add(T obj) {
        if(head == null){
            head = new Node<T>(obj);
            head.next = null;
            tail = head;
            listCount++;
        }
        else{
            if(head.next == null){
                head.next = new Node<T>(obj);
                //head.next.next = null;
                tail = head.next;
                tail.prev = head;
                listCount++;
            }
            else{
                tail.next = new Node<T>(obj);
                tail.next.prev = tail;
                tail = tail.next;
                tail.next = null;
                listCount++;
            }
        }
    }

    @Override
    public void add(int index, T obj) throws IllegalArgumentException {
        // TODO Auto-generated method stub      
        Node<T> temp = new Node(obj);
        Node<T> current = head;

        for(int i = 0; i<=index; i++){
            current = current.next;
        }
        temp.prev = current.prev;
        current.prev = temp;
        current.prev.next = current;

        if(index == 0){
            head = current.prev;
        }
        else if(index == size()+1){
            tail = current.next;
        }

        listCount++;
    }

    @Override
    public void replace(int index, T obj) throws IllegalArgumentException {
        // TODO Auto-generated method stub
        if(index > size() - 1 || index < 0){
            throw new IllegalArgumentException();
        }
        else{
            //get(index)
        }

    }

    @Override
    public T remove() {
        head = head.next;
        listCount--;

        return null;
    }

    @Override
    public T remove(int index) throws IllegalArgumentException {
        // TODO Auto-generated method stub
        if(index > size() - 1 || index < 0){
            throw new IllegalArgumentException();
        }
        else{

            listCount--;
        }

        return null;
    }

    @Override
    public int size() {
        return listCount;
    }

    public static void main(String[] args){
        LinkedList<String> list = new LinkedList<String>();
        list.add("Red");
        list.add("Orange");
        list.add("Yellow");
        list.add("Green");
        list.add("Blue");
        list.add("Purple");

        for(int a = 0; a < list.size(); a++){
            System.out.println(list.get(a));
        }       
//      System.out.println(list.get(0).toString());
//      System.out.println(list.get(1).toString());
//      System.out.println(list.get(2).toString());
//      System.out.println(list.get(3).toString());
//      System.out.println(list.get(4).toString());
//      System.out.println(list.get(5).toString());
        System.out.println("There are " + list.size() + " colors in the list.");

        list.remove();

        for(int b = 0; b < list.size(); b++){
            System.out.println(list.get(b));
        }
//      System.out.println(list.get(0).toString());
//      System.out.println(list.get(1).toString());
//      System.out.println(list.get(2).toString());
//      System.out.println(list.get(3).toString());
//      System.out.println(list.get(4).toString());
        //System.out.println(list.get(5));
        System.out.println("There are " + list.size() + " colors in the list.");

        list.add(0, "Red");
        System.out.println(list.size());

        for(int c = 0; c < list.size(); c++){
            System.out.println(list.get(c));
        }
//      System.out.println(list.get(0).toString());
//      System.out.println(list.get(1).toString());
//      System.out.println(list.get(2).toString());
//      System.out.println(list.get(3).toString());
//      System.out.println(list.get(4).toString());
//      System.out.println(list.get(5).toString());
        System.out.println("There are " + list.size() + " colors in the list.");
    }

    class Node<T>{
        T content;
        Node<T> next;
        Node<T> prev;

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

        public T getContents(){
            return content;
        }

        public void printNode() {
            System.out.print("{" + content + "} ");
        }
    }
}

Here's my console readout if it's of any help:

Red Orange Yellow Green Blue Purple There are 6 colors in the list. Orange Yellow Green Blue Purple There are 5 colors in the list. 6 Red Yellow Green Blue Purple Exception in thread "main" java.lang.NullPointerException at edu.neumont.csc250.LinkedList.get(LinkedList.java:26) at edu.neumont.csc250.LinkedList.main(LinkedList.java:161)

EDIT: main method isolated, as requested:

public static void main(String[] args){
        LinkedList<String> list = new LinkedList<String>();
        list.add("Red");
        list.add("Orange");
        list.add("Yellow");
        list.add("Green");
        list.add("Blue");
        list.add("Purple");

        for(int a = 0; a < list.size(); a++){
            System.out.println(list.get(a));
        }       
//      System.out.println(list.get(0).toString());
//      System.out.println(list.get(1).toString());
//      System.out.println(list.get(2).toString());
//      System.out.println(list.get(3).toString());
//      System.out.println(list.get(4).toString());
//      System.out.println(list.get(5).toString());
        System.out.println("There are " + list.size() + " colors in the list.");

        list.remove();

        for(int b = 0; b < list.size(); b++){
            System.out.println(list.get(b));
        }
//      System.out.println(list.get(0).toString());
//      System.out.println(list.get(1).toString());
//      System.out.println(list.get(2).toString());
//      System.out.println(list.get(3).toString());
//      System.out.println(list.get(4).toString());
        //System.out.println(list.get(5));
        System.out.println("There are " + list.size() + " colors in the list.");

        list.add(0, "Red");
        System.out.println(list.size());

        for(int c = 0; c < list.size(); c++){
            System.out.println(list.get(c));
        }
//      System.out.println(list.get(0).toString());
//      System.out.println(list.get(1).toString());
//      System.out.println(list.get(2).toString());
//      System.out.println(list.get(3).toString());
//      System.out.println(list.get(4).toString());
//      System.out.println(list.get(5).toString());
        System.out.println("There are " + list.size() + " colors in the list.");
    }
4
  • What line does line 26 refer to? Commented Apr 18, 2011 at 3:36
  • what will happen when current.next be null? You will not be able to issue current without getting an exception because current would be null, isn't it ? Commented Apr 18, 2011 at 3:37
  • Also, can you post main code? Commented Apr 18, 2011 at 3:38
  • the main code is in there already but here it is in its isolated form. (in edit) Commented Apr 18, 2011 at 3:41

1 Answer 1

1

When insert(add()) with index 0, the new element will be inserted behind the first element.
However, the head reference points to the newly inserted element rather than the actual head element after the operation is done.
Thus, a NPE will occur when you iterate your list.
In fact, you can get this from the output of the programe. Notice that the third iteration starts at 'Red' and the 'Orange' diappears.


And the implementation of remove() is not good, for it'll lead to 'Memory Leak'. It just moves the 'head' forward without null-ing the element.

Sign up to request clarification or add additional context in comments.

9 Comments

that's why I added the 'if(index == 0){head = current.prev;}' section, though. Why doesn't that work?
thanks for pointing out that problem with my remove method, I just fixed that. :) However, I still don't fully understand why my solution (posted in comment above) doesnt work. :/
@ChrisV: Try to draw a running map. On my paper, it doesn't work.
@ChrisV: After the insertion, current points to the third element. Thus head = current.prev makes the head referring to the second element.
I did this. That's how I was checking it, but it looks like it works when I draw it out. Hence why I don't get why it doesnt work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.