title: linkedlist implementation add method
body: <p>I'm implementing a linked list for a course online, and there seems to be something wrong with my add function.  When I try to add the first element, the Eclipse prints null, and for a second element Eclipse shows an error.  (I'm assuming because the first element was never added, so there can't be a second one.)</p>
  
  <p>This is the implementation of my linked list:</p>
  
  <pre><code>    package textgen;
  
      import java.util.AbstractList;
  
      public class MyLinkedList&lt;E&gt; extends AbstractList&lt;E&gt; {
          LLNode&lt;E&gt; head;
          LLNode&lt;E&gt; tail;
          int size;
  
          /** Create a new empty LinkedList */
          public MyLinkedList() {
  
              size = 0;
  
              head = new LLNode&lt;E&gt;();
              tail = new LLNode&lt;E&gt;();
              head.next = tail;
              tail.prev = head;
  
          }
  
          /**
           * Appends an element to the end of the list
           * @param element The element to add
           */
          public boolean add(E element ) 
          {
  
              add(size, element);
  
              return false;
          }
  
          /** Get the element at position index 
           * @throws IndexOutOfBoundsException if the index is out of bounds. */
          public E get(int index)  throws IndexOutOfBoundsException
          { 
  
              if(index &gt;= this.size){
                  throw new IndexOutOfBoundsException("Your index is out of bounds!");
              }
  
              LLNode&lt;E&gt; lGet = head;
              for(int i = 0; i &lt; index + 1; i++){
                  lGet = lGet.next;
              }
  
              return  lGet.data;   
          }
  
  
          public void printList(){
  
              LLNode lTemp = head;
  
              while(lTemp.next != tail){
                  System.out.println(lTemp.next.data);
                  lTemp = lTemp.next;
              }
          }
  
  
  
  
          /**
           * Add an element to the list at the specified index
           * @param The index where the element should be added
           * @param element The element to add
           */
  
          public void add(int index, E element )  throws IndexOutOfBoundsException
          {
              if(index &gt; this.size){   
                  throw new IndexOutOfBoundsException("Oops!  Out of bounds!");
          }
  
                  else{
                      LLNode&lt;E&gt; nAdd = new LLNode&lt;E&gt;(element);
                      LLNode&lt;E&gt; nIt = null;
  
  
                      if(index &lt;= size/2)  //   if  the index is closer to the start from the beginning of the list
                  {
                      nIt = head;
  
                          for(int i = 0; i &lt; index + 1; i++){
                              nIt = nIt.next;
                          }
                      }
  
  
                  else {
  
                      nIt = tail;
  
                          for(int i = this.size; i &gt; index; i--){
                              nIt = nIt.prev;
                          }
  
                      }
  
                      nIt.prev.next.prev = nAdd;    
                      nAdd.next = nIt.prev.next;
                      nIt.prev.next = nAdd;
                      nAdd.prev = nIt.prev;
  
              size++;
                  }   
  
          }
  
  
          /** Return the size of the list */
          public int size()      
  
          {
              return size;
          }
  
          /** Remove a node at the specified index and return its data element.
           * @param index The index of the element to remove
           * @return The data element removed
           * @throws IndexOutOfBoundsException If index is outside the bounds of the list
           * 
           */
          public E remove(int index) 
          {
              // TODO: Implement this method
  
  
  
              size--;
  
              return null;
          }
  
          /**
           * Set an index position in the list to a new element
           * @param index The index of the element to change
           * @param element The new element
           * @return The element that was replaced
           * @throws IndexOutOfBoundsException if the index is out of bounds.
           */
          public E set(int index, E element) 
          {
              // TODO: Implement this method
              return null;
          }   
      }
  
      class LLNode&lt;E&gt; 
      {
          LLNode&lt;E&gt; prev;
          LLNode&lt;E&gt; next;
          E data;
  
          public LLNode(){
              this.data = null;
              this.prev = null;
              this.next = null;
          }
  
          public LLNode(E e) 
          {
              this.data = e;
              this.prev = null;
              this.next = null;
          }
  
      }
  
  This is the main:
  </code></pre>
  
  <p>package textgen;</p>
  
  <pre><code>public class fixAdd {
  
  
      public static void main(String [] Arg){
  
          MyLinkedList&lt;String&gt;  ll = new MyLinkedList&lt;String&gt;();
          ll.add(0, "happy");
      ll.add(1, "gilda");
          System.out.println(ll);
  
      }
  }
  
  And this is the error printed:
  
  Exception in thread "main" java.lang.NullPointerException
      at textgen.MyLinkedList.get(MyLinkedList.java:57)
      at java.util.AbstractList$Itr.next(Unknown Source)
      at java.util.AbstractCollection.toString(Unknown Source)
      at java.lang.String.valueOf(Unknown Source)
      at java.io.PrintStream.println(Unknown Source)
      at textgen.fixAdd.main(fixAdd.java:11)
  </code></pre>
  
  <p>I've gone over my add method a number of times, and compared it to other implementations I found online, and everything seems in order.  I'm totally confused and would appreciate any help.  Thanks!</p>