Timotheus
Timotheus

Reputation: 2340

Argument vanishes in the middle of the method?

Whenever I run this method the print goes perfectly fine so the argument is passed and not null. Still it gives a NullPointerException when the argument is added to the children ArrayList (which is a part of the class). Why is this?

public void addChild(_Node n){
    System.out.println("Add " + n.getClass().getSimpleName() + " to " + this.getClass().getSimpleName());
    children.add(n);
}

Upvotes: 0

Views: 80

Answers (2)

AbdullahC
AbdullahC

Reputation: 6730

Check to see if children has been instantiated correctly. This is a common mistake beginners make.

Upvotes: 3

Speck
Speck

Reputation: 2299

NullPointerException when the argument is added to the children ArrayList

children is null.

n is not null and you can add null objects to most Lists.

Upvotes: 3

Related Questions