List<Node> nodes = null;
This is not an empty list; it is a reference to a list that is initialized to zero. You want more soon:
List<Node> nodes = new LinkedList<Node>();
or similar. This gives you an empty list.
EDIT: after changing the question, this logic is completely messed up:
Node node1= new Node(first, first); if (nodes==null){ nodes.add(node1); } if (nodes!=null){ if(nodes.contains(node1)){ nodes.add(node1); } }
What you are saying here is that if node == null (which it isnβt now), try adding node to the list. If it is not null, and node1 is already in the list (which is not the case), add it to the list. You can replace the lines above:
if (!nodes.contains(node1)) { nodes.add(node1); }
which says if node does not exist in the list add it.
source share