11

So I have an empty list like so

List<Node> nodes = null;

and then I want to add "Node"s into 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 obviously doing .contains in a null list gives me an exception error, but why does doing

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

also gives me a null pointer error? It seems like empty lists are immutable. How can I still keep a List structure and still build it up from empty?

8 Answers 8

15
List<Node> nodes = null;

This is not an empty list, this is a list reference which is iniitalized to null. You want rather:

List<Node> nodes = new LinkedList<Node>();

or similar. This gives you an empty list.

EDIT: After the change of 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're saying here is that if nodes == null (which it is now not), then try to add the node to the list. If it is not null, and node1 is already in the list (which it is not), then add it to the list. You can replace the lines above with:

if (!nodes.contains(node1)) {
    nodes.add(node1);
}

which says if the node doesn't exist in the list, add it.

Sign up to request clarification or add additional context in comments.

Comments

3

This is because you have not instantiated your object "nodes".

Add the following line and it should work :

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

Also note you cannot instantiate List objects. So you can use something like ArrayList/LinkedList etc.

Comments

1

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

2 Comments

you can't instantiate the List interface :)
Whoops, Should have been ArrayList or similar. I'll edit my answer. Cheers for that.
1

You need to initalize the list first

nodes = new ArrayList<Node>();

for example:

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

But it is really better to have an empty list instead of an not initialzed (null) list.

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

Comments

1

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

Comments

0
nodes = new ArrayList<Node>();

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

3 Comments

I just made the same mistake, you can't instantiate a List, it has to be an ArrayList or LinkedList etc.
of course! Thanks for point it out. Still in C# mode i guess :)
Haha no worries. I got a comment telling me the same thing with my answer. I'm in C++ mode.
0

this is a sure null pointer exception as nodes is null

did you mean

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

Also initialize list

nodes = new ArrayList()

Comments

0

You node is null so this is the error like all said... this is most common programming mistake for getting a null pointer exception. Its better you use findbug plugin with your eclipse; it wont allow you to create this kind of error. Also it will be easier for you to detect the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.