Add to an empty list

So I have an empty list

List<Node> nodes = null; 

and then I want to add "Node" to it

  try { File file = new File("test.txt"); Scanner scanner = new Scanner(file); while (true){ String first= scanner.next(); if (first.equals("-1")){ break; } Node node1= new Node(first, first); if (nodes==null){ nodes.add(node1); } if (nodes!=null){ if(nodes.contains(node1)){ nodes.add(node1); } } 

So it is obvious that .contains in the null list gives me an exception error, but why does

  if (nodes==null){ nodes.add(node1); } 

also gives me null pointer error? Empty lists seem to be immutable. How can I save the list structure and still create it from empty?

+6
source share
8 answers
 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.

+8
source

You must allocate memory to the list: nodes = new ArrayList<Node>(); before adding node. You are trying to add node to the null list, i.e. Add node to nothing.

+1
source

First you need to initialize the list

 nodes = new ArrayList<Node>(); 

eg:

 if (nodes==null){ nodes = new ArrayList<Node>(); nodes.add(node1); } 

But it's really better to have an empty list instead of a list without initialization (null).

So instead: List<Node> nodes = null; have List<Node> nodes = new ArrayList<Node>(); then you can remove the full if and always use nodes.add(node1); , regardless of whether it is the first or second element.

+1
source

if nodes null , you can't do anything about it. Initialize it to new ArrayList<Node>() . Now this is an empty list.

+1
source

This is because you did not create an instance of the nodes object.

Add the following line and it should work:

  if (nodes==null){ nodes = new ArrayList<Node>(); nodes.add(node1); } 

Also note that List objects cannot be created. This way you can use something like ArrayList / LinkedList etc.

+1
source
 nodes = new ArrayList<Node>(); 

Initializing it to a new object (empty) should help.

0
source

this is a true null pointer exception since nodes is null

You meant

 if (nodes!=null){ nodes.add(node1); } 

Also initialize the list

 nodes = new ArrayList() 
0
source

You are node null , so this is an error, like everything said ... this is the most common programming error for getting a null pointer exception . Its best to use the findbug plugin with your eclipse; this will not allow you to create such an error. It will also be easier for you to detect the problem.

0
source

Source: https://habr.com/ru/post/902745/


All Articles