1

So here is my question.In my code I made an object ob, and I created a temp node just to point it to root node.So when I printed the data for temp ,as I expected I got NullPointerException. But after I assign data to root node , I again printed data for temp node this time I was expecting output same as data for root node but instead I again got NullPointerException.

Why is this happening? Isn't temp node is just pointing(Yeah! Yeah! there are no pointers in java).

Here is the code

 abc ob=new abc();
    Node temp=ob.root;
    try {
        System.out.println(temp.data);
    }
    catch (NullPointerException ex)
    {
        System.out.println("Null");
    }
    while(sc.hasNextInt())
        ob.add(sc.nextInt());   //add method is used to add elements in linked list
    System.out.println(temp.data);

And here is node class.

 class Node
{
    int data;
    Node next;
    Node(int data)
    {
        this.data=data;
        this.next=null;
    }
}

Feel free to ask if you don't understand my code And, Please forgive my English.

Full code

    import java.util.Scanner;

class abc
{
    class Node
    {
        int data;
        Node next;
        Node(int data)
        {
            this.data=data;
            this.next=null;
        }
    }
    Node root=null;

    void add(int data)
    {
        Node new_node=new Node(data);
        if(root==null)
            root=new_node;
        else
        {
            new_node.next=root;
            root=new_node;
        }
    }

    public static void main(String ar[])
    {
        Scanner sc=new Scanner(System.in);
        abc ob=new abc();
        Node temp=ob.root;
        try {
            System.out.println(temp.data);
        }
        catch (NullPointerException ex)
        {
            System.out.println("Null");
        }
        while(sc.hasNextInt())
            ob.add(sc.nextInt());   //add method is used to add elements in linked list
        //System.out.println(temp.data);

    }

} 
Aniket Saxena
  • 43
  • 1
  • 6

4 Answers4

1

The variable temp stores null. null is not an object. There is no relation between null in ob and null in temp. Therefore, when the reference to a node is stored in ob the value in temp does not change.

For more info about null, see this.

You can store an empty node in root. In catch block initialize root and store the same reference in temp. (You can also declare a constructor for an empty node)

ob.root = ob.new Node(0);
temp = ob.root;

In add() method, check if root has a value and add a value to data field of root if it is empty.

void add(int data) {
    if (root.data == 0)
        root.data = data;
    else {
        Node new_node = new Node(data);
        new_node.next = root;
        root = new_node;
    }
}
ghoul932
  • 192
  • 1
  • 8
  • Yeah but when I updated root node , the temp should be updated as well because it is still pointing to root node. – Aniket Saxena Mar 23 '19 at 09:28
  • null is not an object and root is only a variable. References are for objects not variables. – ghoul932 Mar 23 '19 at 09:32
  • Nowhere. It just had a null value. See the question I have linked to. – ghoul932 Mar 23 '19 at 10:05
  • Okay so it means temp was not actually pointing to any object.as null is not object . Right? – Aniket Saxena Mar 23 '19 at 10:10
  • Yes because temp does not store a reference to any object. – ghoul932 Mar 23 '19 at 10:13
  • Now I get it . Thanks guys . How do I close this question. – Aniket Saxena Mar 23 '19 at 10:15
  • This code will launch an exception if `root` is not initialized before calling the method `add`. @Aniket Saxena Did you tested this code before accepting the answer? – Louis Mar 23 '19 at 10:23
  • Edit : Initialized root in catch block so that Null is printed when no value has been entered. – ghoul932 Mar 23 '19 at 10:38
  • There can be many ways to initialize root according to the situation. – ghoul932 Mar 23 '19 at 10:40
  • @Louis root is an instance variable so it would also work properly. – ghoul932 Mar 23 '19 at 10:41
  • @Hammad Malik No, bacause at the instruction `root.data == 0` if `root` is null, you're trying to access a property of `null` (`null.data`) and it will launch an exception. This is not going to work if `root` is `null`. – Louis Mar 23 '19 at 10:51
  • root is not null. A reference to a node is stored in it when an object of class abc is made. – ghoul932 Mar 23 '19 at 10:56
  • In the edited post, there would be a NullPointerException so the catch block will be executed. In the post before editing, root was not null so there was no exception. – ghoul932 Mar 23 '19 at 10:59
0

Here: ob.add(sc.nextInt()); you're manipulating a "parent" object, so the expectation, that in child there won't be null is not justified. Looking at definition of Node class, it has no idea of existence of root.

P.S.: Code seems to be missing some parts. For example I have no idea what abc() is.

Andronicus
  • 25,419
  • 17
  • 47
  • 88
0

You get a NullPointerException because you never called the add method and therefore ob.root is null.

Try this code, it should work:

 public static void main(String ar[])
    {
        Scanner sc=new Scanner(System.in);
        abc ob=new abc();
        System.out.print("Enter an integer followed by <enter>: ");
        ob.add(sc.readInt());
        Node temp=ob.root;
        try {
            System.out.println(temp.data);
        }
        catch (NullPointerException ex)
        {
            System.out.println("Null");
        }
        while(sc.hasNextInt())
            ob.add(sc.nextInt());   //add method is used to add elements in linked list
        //System.out.println(temp.data);

    } 

PS: Please, also note that according to Java Naming convenctions, class names should always start with an upper case letter.

Louis
  • 3,592
  • 2
  • 10
  • 18
  • But after updating root node I should also observe changes for temp node. Right? – Aniket Saxena Mar 23 '19 at 08:59
  • I updated my answer according to your code. Check, it should work. – Louis Mar 23 '19 at 09:04
  • Yes, Java works with references. To better understand, take a look at this question: https://stackoverflow.com/questions/5160485/object-vs-reference-in-java – Louis Mar 23 '19 at 09:21
  • What is the need of calling add method. Look when I updated the root node data the temp should be updated as well because it is still pointing to root node, how is temp node is null after updating root node. – Aniket Saxena Mar 23 '19 at 09:31
  • `temp` is a **reference** to an **object**. If you declare `temp = ob.root` and `ob.root` is `null`, then you'll have `temp=null`. The only part of the code where you initialize `root` is in the `add` method, so you have to call it first. – Louis Mar 23 '19 at 10:27
0

Here is a bit of whats going on

public static void main(String ar[])
{
    Scanner sc=new Scanner(System.in);
    abc ob=new abc();
    // the value for root (Node object) for the ob object is null, as you declared in your class

    Node temp=ob.root;
    // You have assigned the null value to the temp reference
    // equivalent to saying Node temp = null;
    try {
        System.out.println(temp.data);
        // This doesnt have anything to refer to so throws the exception
    }
    catch (NullPointerException ex)
    {
        System.out.println("Null");
    }
    while(sc.hasNextInt())
        ob.add(sc.nextInt());   //add method is used to add elements in linked list
    //System.out.println(temp.data);

}

********************* . change to this and you will get the last node in your temp **************

public static void main(String ar[])
{
    Scanner sc=new Scanner(System.in);
    abc ob=new abc();
    // the root value for the ob object is null, as you declared in your class
    while(sc.hasNextInt())
        ob.add(sc.nextInt());   
    // Once you have completd the node entries, the last node is what object ob will hold.
    // and the value will be the root node value

    Node temp=ob.root;

    try {
        System.out.println(temp.data);

    }
    catch (NullPointerException ex)
    {
        System.out.println("Null");
    }


}

******************* To show the difference in initialisation **********

class abc
{
class Node
{
    int data;
    Node next;
    Node(int data)
    {
        this.data=data;
        this.next=null;
    }
}
Node root=new Node(0);
Node root2 = null;

void add(int data)
{
    Node new_node=new Node(data);
    if(root==null)
        root=new_node;
    else
    {
        new_node.next=root;
        root=new_node;
    }
}

public static void main(String ar[])
{
    Scanner sc=new Scanner(System.in);
    abc ob=new abc();
    // the root value for the ob object is null, as you declared in your class
    //while(sc.hasNextInt())
    //  ob.add(sc.nextInt());   
    // Once you have completd the node entries, the last node is what object ob will hold.
    // and the value will be the root node value

    Node temp=ob.root;

    try {
        System.out.println(temp.data);
        System.out.println(ob.root);
        System.out.println(ob.root2);

    }
    catch (NullPointerException ex)
    {
        System.out.println("Null");
    }


}

}

****** Output will be ****

0
nodeSoulutin.abc$Node@6b71769e
null
VSB
  • 317
  • 2
  • 10
  • Yeah but when I updated root node , the temp should be updated as well because it is still pointing to root node.How is temp is still null. – Aniket Saxena Mar 23 '19 at 09:28
  • The temp is not pointing to root node, it has the value null. So it cant do what you expect it to do – VSB Mar 23 '19 at 09:32
  • I am editing the code, so you will be asked to create the nodes first, and the last node will be kept in your temp variable, which will get printed – VSB Mar 23 '19 at 09:34
  • But for experimentation purposes what is wrong in my code. I can't understand why the temp node is updating as well when root node was updated.Look when I created a temp node without assigning any memory to it , in that scenario it meant that temp node can point to any other node object. So , in my code temp was pointing to root node for ever so, when I update root node the temp node should be updated as well/ – Aniket Saxena Mar 23 '19 at 10:07
  • What you want can be achieved when you have a reference to the root. so change Node root=new Node(0); rather than Node root2 = null;, as the first will give you a reference in memory whereas the later will assign the value null to the variable. I am updating the full code to print the value the two will hold. – VSB Mar 23 '19 at 10:16
  • Yeah I just get it what you all were trying to explain.How do I close this question.Thanks for helping. – Aniket Saxena Mar 23 '19 at 10:17
  • Vote up the one answer you think helped you to get an answer, rest no other action required. – VSB Mar 23 '19 at 10:20