Please help me with the double linked list, when I write the command in terminal to test main class by my source code the cmd.exe throw me:
Exception in thread "main" java.lang.NullPointerException
at homework.List.add(Homework2.java:19)
at homework.Homework2Main.main(Homework2Main.java:20)
In 19 line is something wrong in source code, I tag him. Please I need help.
Code:
package homework;
class Node {
Node previous, next;
int contents;
Node(int val) {
this.contents = val;
}
}
class List {
Node first;
void add(int num) {
Node x = new Node(num);
if(first.next == null) { //this line
this.first = x;
}else{
Node current = this.first;
boolean inList = false;
while(current.next != null) {
if(current.contents == num) inList = true;
current = current.next;
}
if(!inList) {
current.next = x;
x.previous = current;
}
}
}
void remove(int num) {
Node current = this.first;
while(current.next != null) {
if(current.contents == num) {
if(current.previous != null) {
current.previous.next = current.next;
}
if(current.next != null) {
current.next.previous = current.previous;
}
}
current = current.next;
}
}
boolean contains(int num) {
Node current = this.first;
while(current.next != null) {
if(current.contents == num) return true;
current = current.next;
}
return false;
}
}