Category

vavr

No NullPointerException again

How many bugs we get in the course of our lives as developers because of NullPointerExceptions! If you never want to get a NPE again, read this article.
NullPointerExceptions can be annoying for everyone trying to deliver his or her stuff. After teasing the feature, it is totally maddening when the quality guys report back a bug due to a NPE.
So how can we clear our code of a NullPointerException? Read further and you will find some solutions to choose from.

Use java.util.Optional with java 8

If you use Java 8 already, you can try to use java.util.Optional. It is a class that encapsulates an optional value.
The optional goals are that null checks are not required, there are no more NullPointerExceptions at run-time, and we can develop clean and neat APIs and no more boilerplate code.

– To create an optional object:

– To check if the object is empty and any value exists or not:

– To create a value with static API:

– The method for how to escape the NullPointerException dilemma is that you use the ofNUllAble API:

In this case we pass in a null reference, so we will not get the NullPointerException.

– The alternative case with orElse() / orElseGet() / orElseThrow:

Here you can give the object the choice that in case of null, it replaces the value object with the name SAM.
With orElseThrow() you throw an exception:

– To get the value:

– To filter the elements with filter():

The optional class in JDK 9 has been improved and the following changes have been made:

Allows execution of an action in a positive or negative case.

Converts the optional to a stream.

Allows linking of multiple calculations in an elegant way.

The purpose of optional is not to replace every single null reference in your codebase but rather to help design better APIs in which users can tell whether to expect an optional value. If you do not have the possibility to use JDK 8 or 9, you still have the option to use guava:

Guava:
To add a dependency on Guava using Maven, use the following:

You can use Optional with Guava. The optional with Guava is an immutable object used to contain a not-null object. Optional object is used to represent null with absent value. This class has various utility methods to facilitate the code to handle values as available or not available instead of checking null values.

It offers methods such as: absent(), asSet(), get(), isPresent(), of(T reference) and orNull()

//Optional.fromNullable – allows passed parameter to be null.

//Optional.of – thro lPointerException if passed parameter is null

You will find more information in the API-Doc: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/base/Optional.html

Vavr
Vavr is an awesome, functional library for Java 8+ that provides persistent data types and functional control structures.

To add a dependency on Vavr using Maven, use the following:

In addition to the common met- hods, vavr also offers an Option. Option in vavr is a monadic container type which represents an optional value. Instances of Option are either an instance of some objects or none.
The main goal of Option is to eliminate null checks in our code by leveraging the Java type system.
Option is an object container in Vavr with a similar end goal like Optional in Java 8. Vavr’s Option implements Serializable, Iterable, and has a richer API.