You might have written extremely excellent business logic, algorithms, followed coding guidelines etc. but if code is prone to NULL POINTER exception, it is not worthwhile having the code.
MUST AVOID NULL POINTERIn my experience, I have seen that programmers actually follow the approach of GO-Lucky with this. They believe that their code is perfect & any particular section cannot generate the NULL POINTER. These proud sections are the real source of NULL POINTER.
Make your code 100 % error free.
Follow the below mention checkpoints that can help programmers to get rid of Null pointers.
Usage of EQUALS Method
Consider the sample code
This statement looks pretty good & error free but actually it is most common mistake we all do & most dangerous statement in java. This will throw a NULL POINTER exception if values of status is NULL as NULL cannot invoke equalIgnoreCase().
Remedy: Change the statement to:
In this case, first attribute is of type String [In-Progress is string], so it is capable to invoking equalignoreCase(). Now even if status is NULL, code will work fine as String value can be compared to NULL or Not Null values.
So this small tweak will make your code happy!
EMPTY or NULL Collection
There is huge difference between EMPTY & NULL collections e.g. List, Map etc. Empty List is a list that does not contain any elements i.e. LIST.size is 0. Null List is a list that has all elements NULL. So never initialise LIST to Null as
but instead do
Same way for other collection type, we have Collections.EMPTY_MAP
Treat Null Pointer Gracefully
Neither throw or throws Null Pointer exception. This also states true for the fact that Null Pointer exception is an unchecked exception also. There are much better ways to handle potential null pointer exceptions. So do not handle exception in below way :
If your business logic allows userDTO == null then follow the below approach:
Hierarchical Invoking of methods
This coding practise is also one of the potential causes of exception. We generally call methods hierarchically in a single statement like:
Above statement can throw null pointer exception if service.getSession(“key”) is null , so there is need to refractor the code as
Using Third-party Classes.
As Java is growing fast & lots of open source libraries available in market , consider this as a rapid development technique. org.apache.commons.lang Package provides a StringUtils Class that exposes utility methods to avoid null pointer exception. Exposed methods : IsEmpty(), StringUtils. IsBlank(), StringUtils.equals(). All these methods are Null pointer safe.
Dimit Chadha CORE-JAVA