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