when I run my TreeNode code, the compiler say the TreeNode that be called by countUnivalSubtrees method is null. But, when I run debug mode, it tells me that the Treenode I create is not null. The object, foolbar, of class Treenode has its root, left branch and right branch. So why when the foolbar is called, the method is received a null object?
1.The Treenode code
package lc_250;
public class TreeNode {
int val;
TreeNode root;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
public void put(int val){
this.root = put(this.root, val);
}
private TreeNode put(TreeNode node_now, int val){
if (node_now == null) return new TreeNode(val);
else if (val < node_now.val) node_now.left = put(node_now.left, val);
else if (val > node_now.val) node_now.right = put(node_now.right, val);
else node_now.val = val;
return node_now;
}
public static void main(String[] args) {
TreeNode foolbar = new TreeNode(3);
foolbar.put(5);
foolbar.put(6);
foolbar.put(3);
foolbar.put(2);
foolbar.put(4);
foolbar.put(9);
foolbar.put(8);
foolbar.put(7);
cson250 foolclass = new cson250();
int num = foolclass.countUnivalSubtrees(foolbar);
}
}
- The cson250 code
package lc_250;
public class cson250 {
public int num = 0;
public int countUnivalSubtrees(TreeNode root) {
count(root);
return num;
}
public boolean count(TreeNode root){
if (root == null) System.out.println("bro Treenode is null");
boolean left = count(root.left);
boolean right = count(root.right);
if ( root.left == null && root.right == null){
num ;
return true;
}
if (left&&right){
if (root.left!=null && root.left.val != root.val){
return false;
}
if (root.right!= null && root.right.val != root.val){
return false;
}
num ;
return true;
}
return false;
}
}
CodePudding user response:
I would suggest, if you want to see your tree node as null in debug mode, make your code look like this, so as to put debug point when root == null.
public boolean count(TreeNode root){
if (root == null)
System.out.println("bro Treenode is null");
This way you will see your root in null at some point in your program flow. You can put a debug point at line where you are printing System.out.println("bro Treenode is null");
CodePudding user response:
You are showing a different point in the debugger then where the NPE is happening. This is the reason why your watched fields don't show that root == null
.
In every leaf of your tree, the root
will be null
, so when you encounter the first leaf (should be the node with val "2", since this is the smallest value in your list and you put the smaller values on the left nodes and you check the left nodes first in your count
method), you will first execute
if (root == null) System.out.println("bro Treenode is null");
but then your code continues execution of
boolean left = count(root.left);
and there the NullPointerException
occurs, because root
of course still is null
and so you cannot access root.left
.
All your checks to prevent the recursive call happen after the call, so they don't really help to stop accessing a root
that is null
.