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.)
This is the implementation of my linked list:
package textgen;
import java.util.AbstractList;
public class MyLinkedList<E> extends AbstractList<E> {
LLNode<E> head;
LLNode<E> tail;
int size;
/** Create a new empty LinkedList */
public MyLinkedList() {
size = 0;
head = new LLNode<E>();
tail = new LLNode<E>();
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 >= this.size){
throw new IndexOutOfBoundsException("Your index is out of bounds!");
}
LLNode<E> lGet = head;
for(int i = 0; i < 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 > this.size){
throw new IndexOutOfBoundsException("Oops! Out of bounds!");
}
else{
LLNode<E> nAdd = new LLNode<E>(element);
LLNode<E> nIt = null;
if(index <= size/2) // if the index is closer to the start from the beginning of the list
{
nIt = head;
for(int i = 0; i < index + 1; i++){
nIt = nIt.next;
}
}
else {
nIt = tail;
for(int i = this.size; i > 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<E>
{
LLNode<E> prev;
LLNode<E> 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:
package textgen;
public class fixAdd {
public static void main(String [] Arg){
MyLinkedList<String> ll = new MyLinkedList<String>();
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)
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!