Obviously one of the greatest banes of Java programming is nulls and null-pointer exception. What design patterns are there that don't add too much to your code but reduce the problem of sand null-pointer exceptions?
8 Answers
Null Object pattern. Look at Optional class from google-guava library.
Their wiki has an article on using and avoiding nulls.
Just get used to not returning null objects on your methods.
public MyObject bohemianRhapsody(){
try {
if(isThisTheRealLife())
return new MyObject("is this just fantasy");
else
return new MyObject("caught in a landslide, no escape from reality");
} catch(ExampleCatchableException e){
return new MyObject(""); // instead of return null
}
}
...
System.out.println(bohemianRhapsody()); // will never print null
Also (kind of) referred to as the Null-Object pattern.
Why do you want to avoid null pointer exception? Getting null
when expecting something else it is one of the first indications something is wrong when you write code. Things like Null Object Pattern
should be use when you are sure it is adequate. One of the biggest disadvantages of design pattern are their abuse\misuse.
EDIT:
I think the best way to reduce null return will be to increase usage of exceptions. Think about a List returning a null object when you try to access to the element at index -1, you will be using things like
if( list.get(-1).equals(nullObject))
which is even worse. I believe it is better to raise an exception when the arguments are either unexpected or incompatibles.
There are some helpfull annotations designed for thiese purposes by IntellyJ: @Nullable and @NotNull
Detecting probable NPE’s
I guess it's a matter of taste, but I would say that the greatest banes of languages like Smalltalk/Objective-C is that they DON'T have null and that there AREN'T any NullPointerExceptions. I'm not a fan of "Null object pattern", in fact I detest it with passion as I find it makes troubleshooting and finding bugs so much harder without any real benefit. Null
has a meaning (often bad) and should not be treated like any other generic state.
Like has been mentioned in this thread, NPE are really powerful and a good way to catch syntactical errors or bad coding early on and if (obj != null)
is pretty straight-forward. If your application starts generating NPE at runtime, you've done something wrong and you need to fix it. Don't try to hide the exception, fix whatever it is that is CAUSING the exception.
These methods (see also Apache commons-lang) can reduce the burden of null checks in the cases of String processing by encapsulating them in methods that preserve semantic meaning of code:
public static boolean isBlank(final String str)
{
return (str == null) || str.isEmpty();
}
public static boolean isNotBlank(final String str)
{
return !StringUtils.isBlank(str);
}
public static boolean isEqual(final String s1, final String s2)
{
if (s1 == s2) { return true; }
if ((s1 == null) || (s2 == null)) { return false; }
return s1.equals(s2);
}
public static boolean isNotEqual(final String s1, final String s2)
{
return !StringUtils.isEqual(s1, s2);
}