Doing null checks in Java 8 - A better way.
NullPointerExceptions are like nightmares, most of the time it happens due to a simple null check which we are missing before start using a reference variable which refer to nothing. We think we all have to deal with carefully while writing even a small piece of java code. But solving this problem is really simple. Adding a simple if condition which does a condition check. Solving the problem (Imperative). As it I mentioned about the best way to avoid this problem is to do a null check before using the object, especially when the object is not created by yourself. As preventive measure most of time we may need to check the reference of a variable before passing that to a method as an argument. For an instance bar is an object. Before calling a method foo,We need to check whether variable bar has valid reference or not as shown below. if(bar != null) { obj.foo(bar); } We use this technique everywhere. But things look weird w