Java Program to demonstrate NullPointerException

Program

public class NullPointerExceptionDemo
{
   public static void main(String args[])
   {
	try{
		String str = null;
		System.out.println(str.length());
	}
        catch(NullPointerException e){
		System.out.println("Catching NullPointerException");
	}
   }
}

Output

Catching NullPointerException