I am trying to make a LinkedSet object class that implements a modified Set interface. I am getting a NullPointerException when I try and check if the firstNode is pointing to null or not. I'm not really sure how to solve this issue.
Here is relevant code.
Constructor for overall Set object
public class LinkedSet<T> implements Set<T> {
private Node firstNode;
public LinkedSet() {
firstNode = null;
} // end Constructor
Method that is holding me up
public int getSize() {
int size = 1;
Node current = firstNode;
while ((current.next) != null) {
size++;
current = current.next;
}
return size;
} // end getSize()
isEmpty() method
public boolean isEmpty() {
Node next = firstNode.next; //Get error here
if (next.equals(null)) {
return true;
}
return false;
} // end isEmpty()
Here is private inner class for Node objects
private class Node {
private T data;
private Node next; //Get Error here
private Node(T data, Node next) {
this.data = data;
this.next = next;
} // end Node constructor
private Node(T data) {
this(data, null);
}// end Node constructor
} // end Node inner Class
And lastly here is the main tester method.
public class SetTester {
public static void main(String[] args) {
LinkedSet<String> set = new LinkedSet<String>();
System.out.println(set.getSize()); //Get error here
}
}