@NotNull helping to fight the battle with NPE’s

You may have come across the @NotNull & @Nullable Intellij IDEA annotations but should you be using them in your day to day coding and if you were to, what benefits would they give?

Well, simply put, these annotations give Intellij IDEA an understanding if a field, parameter or method can reference (or return) a null type and if the IDE spots that the contract is being violated then it will display the violation to the developer and in turn, where NullPointerException’s may occur

The Annotations that can be used are;

@Nullable – the field/method can reference/return a reference of null.

@NotNull – the field/method cannot reference/return a reference of null.

And these annotations can be applied to methods, fields, local variables and parameters.

For example, this code snippet;

@Nullable
public String returnsANull() {
 return null;
 }

Will not show any issues as the @Nullable annotation denotes that a method is allowed to return a null reference. However;

@NotNull
public String returnsANull() {
 return null;
 }

Will show the following inspection:

‘null’ is returned by the method declared as @NotNull” and the reason is that having the @NotNull annotation applied to a method that has a null reference returned and this violates the contract set out for that class.

The above use case is a simple example but imagine a complex hierarchy in your codebase as this is where you get true benefits from intellij IDEA as it will analyze your code paths and the IDE finds that you have violated the contract then it will inform the developer before the NPE is ever thrown!

Another feature that intellij IDEA provides is run-time checking which means that if, at runtime a null parameter is assigned then a the following exception will be thrown “Exception in thread “main” java.lang.IllegalArgumentException: Argument 0 for @NotNull parameter of SimpleClass.notNullableOuput must not be null”. You may ask what the benefits of this (NPE are RuntimeExceptions after all) but with this approach you receive clear reporting of the violation at the point of where it has occurred and not have a NPE nested several layers deep in your execution path..

 On top of all this, Intellij IDEA also an option to scan your entire code base to “infer nullity” on existing methods, fields, local variables and parameters so it can take the work out of retro-fitting this feature.

Ideally these should be standard in the Java API (there is talk it may happen) & another downside is that they do make your codebase more verbose (there are options to externalise these annotations) but I believe this is a price worth paying and you will get payback the more of your code that is defined. Furthermore, I assure you that you will get a little buzz when you see first hand intellij inform you of a possible NPE and it is right…

I will leave you with a quote from C.A.R Hoare (quicksort guy) who is credited for inventing null.

Enjoy!

I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s