How To Waive NPE, The NullPointerException in Your Code Flow?

NullPointerException
String str = null;if (str.equals(“abc123”)) { // NPE will be thrown at this line!System.out.println(“The values are equal!”);}else{System.out.println(“The values are NOT equal!”);}
String str = null;if (“abc123”.equals(str)) { // NO NPE will be seenSystem.out.println(“The values are equal!”);}else{System.out.println(“The values are NOT equal!”);}
String str1 = null;String str2 = null;// NO NPE surely your code will not stop
if (Objects.equals(str1, str2)) {
System.out.println(“The values are equal!”);}else{System.out.println(“The values are NOT equal!”);

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store