1

I am solving the question Link and the logic is very easy. But I found weird behavior when I executed the code. As far as I know in java, if I give any object from main to any method and that method modifies object,then the object from main is modified as it was being referenced. But I am not getting why this is not happening in my code and it is always giving nullpointerexception. Below is my code.`

import java.util.*;
import java.lang.*;
import java.io.*;

 class GFG {

    static class Node
    {
        int val;
        Node left,right;
        Node(int val)
        {
            this.val=val;
            this.left=null;
            this.right=null;
        }
    }
    static int n;
    static int[] a;

    public static void insert(Node root,int left,int right)
    {
        int num=right-left+1;
        root=new Node(a[left+num/2]);
        if(num==1)
            return;
        insert(root.left,left,left+num/2-1);
        insert(root.right,left+num/2+1,right);
    }

    public static void preorder(Node root)
    {
        System.out.print(root.val+" ");
        if(root.left!=null)
            preorder(root.left);
        if(root.right!=null)
            preorder(root.right);
    }

    public static void main (String[] args) {

        Scanner in=new Scanner(System.in);
        int tc=in.nextInt();
        while(tc-->0)
        {
            n=in.nextInt();
            a=new int[n];
            for(int i=0;i<n;i++)
            {
                a[i]=in.nextInt();
            }
            Node root=null;
            insert(root,0,n-1);
            preorder(root);
        }

    }
}`
Patel Parth
  • 315
  • 5
  • 16
  • 1
    You are not passing any object to `insert`, in case that was what you meant. So there isn’t any object for it to modify. – Ole V.V. May 21 '18 at 07:07
  • 1
    `root=new Node(a[left+num/2]);` in `insert` does not affect the root object passed (but anyway you are passing only a null here) – Thiyagu May 21 '18 at 07:07

2 Answers2

2

As far as I know in java, if I give any object from main to any method and that method modifies object,then the object from main is modified as it was being referenced

That's true, but it's not what you are doing in your code. You are passing a variable, holding a null value, to your insert method. Your insert method doesn't change the object referenced by the variable (and it can't, since no object is referenced by null), it assigns a new value to that variable. That new value is not passed to the main method, so the main method's root variable remains null.

Your insert method has to return the Node instance it creates, and your main method should assign the value returned by that method to the root variable.

You can change your insert method to:

public static Node insert(int left,int right)
{
    int num=right-left+1;
    Node root=new Node(a[left+num/2]);
    if(num > 1) {
        root.left = insert(left,left+num/2-1);
        root.right = insert(left+num/2+1,right);
    }
    return root;
}

And call it with:

Node root = insert(0,n-1);
Eran
  • 387,369
  • 54
  • 702
  • 768
  • 2
    **LOL** - The answer I had in progress when yours and Kayaman's appeared *literally* started with exactly the same blockquote and exactly the same opening *"That's true, but it's not what you're doing..."* – T.J. Crowder May 21 '18 at 07:08
  • 2
    Certainly an example where great minds think alike. – Ole V.V. May 21 '18 at 07:17
  • I understood your answer(I was not knowing things you stated in answer before). Still have one question.When root variable which was null is passed to insert and new object is referenced to it, will it be method specific(means will it get destroyed when method is over)? – Patel Parth May 21 '18 at 07:22
  • 1
    @Parth yes, it's a local variable, so if that variable is the only variable holding a reference to that object, it will become eligible for garbage collection once the method returns. – Eran May 21 '18 at 07:24
2

You're not passing an object to insert, you're passing null so there's no object to modify.

Since Java doesn't pass by reference, you can't pass a null root in there and expect root to be non-null afterwards.

Kayaman
  • 72,141
  • 5
  • 83
  • 121