Null Pointer exception using try-catch block in java

Write a program to handle the Null Pointer exception using try-catch block in java.

package Exception_demo;

import java.util.*;
public class nullpointer_soln 
{
    public static void main(String args[])
    {
        Scanner s=new Scanner(System.in);
        try
        {
            String s1=null;
            System.out.println(s1.length());
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *