Check out example codes for "why we get null pointer exception in java". It will help you in understanding the concepts better.

Code Example 1

n=10;

Code Example 2

/**
  * @param obj An optional foo for ____. May be null, in which case 
  *  the result will be ____.
  */
public void doSomething(SomeObject obj) {
    if(obj == null) {
       //do something
    } else {
       //do something else
    }
}

Code Example 3

// To use randomUUID function. 
import java.util.UUID; 
import java.io.*; 
  
class Singleton 
{ 
    // Initializing values of single and ID to null. 
    private static Singleton single = null; 
    private String ID = null; 
  
    private Singleton() 
    { 
        /* Make it private, in order to prevent the  
           creation of new instances of the Singleton 
           class. */
  
        // Create a random ID 
        ID = UUID.randomUUID().toString(); 
    } 
  
    public static Singleton getInstance() 
    { 
        if (single == null) 
            single = new Singleton(); 
        return single; 
    } 
  
    public String getID() 
    { 
        return this.ID; 
    } 
} 
  
// Driver Code 
public class TestSingleton 
{ 
    public static void main(String[] args) 
    { 
        Singleton s = Singleton.getInstance(); 
        System.out.println(s.getID()); 
    } 
}

Learn ReactJs, React Native from akashmittal.com