当对象处于调试模式时,该对象不是null,而是通过方法调用时为空?

发布于 2025-01-21 08:12:48 字数 2456 浏览 0 评论 0 原文

当我运行treenode代码时,编译器说由CountunivalSubtrees方法调用的treeNode为null。但是,当我运行调试模式时,它告诉我,我创建的treenode不是零。类Treenode的对象,foolbar具有其根,左分支和右分支。那么,为什么当名调用doombar时,将收到该方法的空对象呢?

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);
    }
}
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;
    }

}

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?

enter image description here

enter image description here

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);
    }
}
  1. 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;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神仙妹妹 2025-01-28 08:12:48

您在调试器中显示的观点与NPE发生的位置不同。这就是为什么您的手表字段不显示 root == null 的原因。

在树的每一个叶子中, root 将为 null ,因此当您遇到第一片叶子时(应该是带有val“ 2”的节点,因为这是最小的列表中的值,然后将较小的值放在左节点上,然后在 count 方法中选中左节点,您将首先执行,

if (root == null) System.out.println("bro Treenode is null");

但您的代码继续执行

boolean left = count(root.left);

,并在那里执行 nullpointerexception 发生,因为 root 当然仍然是 null ,因此您无法访问 root.left.left

您的所有检查以防止汇总后发生递归电话,因此它们实际上并没有帮助停止访问 root null

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.

苏佲洛 2025-01-28 08:12:48

我建议,如果您想在调试模式下将树节点视为null,请使您的代码看起来像这样,以便在root == null时放置调试点。

public boolean count(TreeNode root){
    if (root == null) 
        System.out.println("bro Treenode is null");

这样,您将在程序流中的某个时刻看到词根。
您可以在打印系统的线上放置一个调试点。

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");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文