简体   繁体   中英

I don't understand why all the data gets printed in the arrange method correctly but does not get printed in the print method

In tree class there is a method called arrange() . When I print any node value within the arrange() method the value comes to be accurate, but when I use the method print() in tree class I get a NullPointerException . Please help!

public class Node {
    Node lnode;
    Node rnode;
    int data;

    public Node(String d) {
        data = Integer.parseInt(d);
        this.rnode = null;
        this.lnode = null;
    }
}

public class tree {

    public void arrange(String s[], int n, Node r) {
        Node root = new Node(s[0]);
        for (int i = 1; i < n; i++) {
            r = root;
            if (Integer.parseInt(s[i]) > root.data && root.rnode == null) {
                root.rnode = new Node(s[i]);
            } else if (Integer.parseInt(s[i]) < root.data && root.lnode == null)
                root.lnode = new Node(s[i]);

            while (!(r.rnode == null) && (Integer.parseInt(s[i])) > r.data) {

                r = r.rnode;
                if (Integer.parseInt(s[i]) > r.data && r.rnode == null)
                    r.rnode = new Node(s[i]);
                else if (Integer.parseInt(s[i]) < r.data && r.lnode == null)
                    r.lnode = new Node(s[i]);
            }
            while (!(r.lnode == null) && (Integer.parseInt(s[i])) < r.data) {
                r = r.lnode;
                if (Integer.parseInt(s[i]) > r.data && r.rnode == null)
                    r.rnode = new Node(s[i]);
                else if (Integer.parseInt(s[i]) < r.data && r.lnode == null)
                    r.lnode = new Node(s[i]);

            }
        }
        System.out.println(root.rnode.data);
    }

    public void print(Node r) {
        System.out.println(r.rnode.data);
    }

}

Main method:

Node root;
int n;
System.out.println("Enter the number of elements you want to enter into the bst: ");
Scanner sc = new Scanner(System.in);
n = Integer.parseInt(sc.nextLine());
System.out.println("Enter the elements you want to enter into the bst: ");
Scanner s = new Scanner(System.in);
String st[] = new String[n];
st = s.nextLine().split(" ");
root = new Node(st[0]);
tree t = new tree();
t.arrange(st, n, root);
t.print(root);

The variable root is a reference to a Node object, which is passed by value to the arrange method. This means that every assignment you make to r inside arrange (that is, every time you use the = operator) will only last as long as you stay inside that method.

When your method returns, the value of root will be the same as before, thus triggering your NullPointerException when you try to access r.rnode.data .

Now that you know the problem, I would suggest you try to find a solution on your own. As the comments suggested, it is not that difficult!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM