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

    }

} 
6
  • From what I'm seeing, the problem comes from the abc class. Can you post the code? Commented Mar 23, 2019 at 8:43
  • So what does ob contain after calling the add method on it? Please dump the content. Commented Mar 23, 2019 at 8:47
  • @Louis Hi Louis I just edited my question. Please check it out. Commented Mar 23, 2019 at 8:52
  • @arkascha I just edited my question please check it out Commented Mar 23, 2019 at 8:54
  • 1
    Thanks for the quick comeback! And welcome to upvote privileges :-) Commented Jun 16, 2019 at 6:44

4 Answers 4

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;
    }
}
Sign up to request clarification or add additional context in comments.

13 Comments

Yeah but when I updated root node , the temp should be updated as well because it is still pointing to root node.
null is not an object and root is only a variable. References are for objects not variables.
Nowhere. It just had a null value. See the question I have linked to.
Okay so it means temp was not actually pointing to any object.as null is not object . Right?
Yes because temp does not store a reference to any object.
|
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.

1 Comment

The Node class does not need to know about a root property. You miss read the code.
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.

5 Comments

But after updating root node I should also observe changes for temp node. Right?
I updated my answer according to your code. Check, it should work.
Yes, Java works with references. To better understand, take a look at this question: stackoverflow.com/questions/5160485/object-vs-reference-in-java
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.
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.
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

7 Comments

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.
The temp is not pointing to root node, it has the value null. So it cant do what you expect it to do
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
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/
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.