I'm trying to implement a LinkedList with an add method. As you can see, I've created a LinkedList class and initialized it with a nodes
attribute that's an ArrayList
containing String
s.
The add
function should simply take a value and append it to the end of nodes
. Instead, I'm getting a NullPointerException.
What am I missing here?
public class LinkedList {
public static ArrayList<String> nodes;
public LinkedList() {
ArrayList<String> nodes = new ArrayList<String>();
System.out.print(nodes);
}
public ArrayList<String> add(String value) {
nodes.add(value);
return nodes;
}
public static void main(String [] args) throws IOException
{
LinkedList list = new LinkedList();
list.add("B");
}
}