Saturday, January 5, 2013

How to through and catch a NullPointerException in Java?

Simple example to show how to through and catch a NullPointerException in Java.

class NullPointer{
    public static void main(String args[]){
        String s=null;

            try{
                System.out.println(s.length());
            }
            catch(NullPointerException e){
                System.out.println("Null Pointer Exception caught by code");
            }
    }
}

No comments:

Post a Comment