kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
Exception in thread "main" java.lang.NullPointerException
at KW.main(KW.java:6)
kodingwindow@kw:~$
Java program to handle the NullPointerException
KW.java
classKW{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
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
String is null, hence, Unable to find the length.
kodingwindow@kw:~$
Dear User, Thank you for visitng KodingWindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.