0
How can I solve Exception in thread âmainâ java.lang.NullPointerException error [duplicate]
My code is: https://code.sololearn.com/#java The error message happens in line: for(TreeNode<String> node: this.children ) This method is to get the leafNode of a tree. Can someone help me to fix my error? thanks!
10 Answers
+ 1
so how can i fix this?
0
The link is to all the Java codes in SoloLearn.
NullPointerException means that you have a null variable which is used as if its not null, which means that this.children is null.
0
add an if statement to check if children is null
0
it will be inside the for loop?
0
Yes
0
I did like below but it does not work
for(TreeNode<String> node: this.children ){
if(node.children.isEmpty()){
nodes.add(node);
} else {
nodes.addAll(getLeafNodes());
}
0
do == null
0
if(node.children() == null ) ??
0
i fixed it. Thanks!
0
Mark my answer as best if it helped to solve your problem :-)