-4

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);
tobias_k
  • 81,265
  • 12
  • 120
  • 179

1 Answers1

0

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!

burubum
  • 640
  • 1
  • 6
  • 18
  • How can access r.rnode.data outside the arrange method? Should i create a separate node while assigning the value (that is, every time you use the = operator) ? – Hitesh KHK Jul 09 '16 at 15:44
  • It is not clear what you are aiming at. Should the `arrange` method modify the `r` parameter? In that case, you cannot use assignments, but you can still modify `r` member variables (see [link](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value)). Should the `arrange` method calculate something? Then `return` the result. – burubum Jul 09 '16 at 16:05