Ich baue grade eine LinkedList
Bei meiner enqueue Funktion bekomme ich eine Nullpointer Exception scheinbar da mein head.next auf einen null Wert linkt, ich hab allerdings keine Ahnung wie ich das ändern kann, hat da jemand nen Tipp für mich?
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package textprocessing;
import textprocessing.Queue.EmptyQueueException;
/**
*
* @author ich
*/
public class LinkedList<Item> implements Queue<Item> {
private Node head;
protected int size;
public LinkedList(){
head = null;
size = 0;
}
protected class Node{
Item item;
Node next;
Node(Item item, Node next){
this.item = item;
this.next = next;
}
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public void enqueue(Item item) {
if (head == null){
head = new Node(item, head.next);
}
else{
head.next = new Node(item,head.next);
size++;
}
}
public Item dequeue() throws EmptyQueueException {
if (isEmpty()){throw new EmptyQueueException(); }
Node item = head;
head = head.next;
head.next = head.next.next;
return item.item;
}
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<String>();
list.enqueue("Bier");
list.enqueue("Alkohol");
list.enqueue("Schnaps");
list.enqueue("Jaegermeister");
list.dequeue();
System.out.print(list);
}
}
Bei meiner enqueue Funktion bekomme ich eine Nullpointer Exception scheinbar da mein head.next auf einen null Wert linkt, ich hab allerdings keine Ahnung wie ich das ändern kann, hat da jemand nen Tipp für mich?