godarda@gd:~$ javac GD.java godarda@gd:~$ java GD
Exception in thread "main" java.lang.NullPointerException
at GD.main(GD.java:6)
godarda@gd:~$
Java program to handle the NullPointerException
GD.java
classGD{publicstaticvoidmain(Stringargs[]){Strings=null;try{System.out.println("Length: "+s.length());}catch(NullPointerExceptione){System.out.println("String is null, hence, Unable to find the length.");}}}
Output
godarda@gd:~$ javac GD.java godarda@gd:~$ java GD
String is null, hence, Unable to find the length.
godarda@gd:~$
Dear User, Thank you for visitng GoDarda. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.