# Avoiding Null Anti Patterns

[CG]jupoulton
8,797 views

## Require non null parameters

One of the first things you can do to avoid null-related problems is to require that given values be not null by using the static method Objects.requireNonNull(variable, errorMessage).

If you elect not to use this method, your code will have to check for null every time the given value should be used which is error-prone and confusing. If a null manages to slip into your code, your program may start behaving in mysterious and unexpected ways.

By requiring non-null values from the very start of a method's execution, you will easily isolate the problem when dealing with a NullPointerException. This makes debugging far easier than if the program crashes several hundred lines later.

You may also attach a custom error message to the NPE by using Objects.requireNonNull.

Use Objects.requireNonNull
// { autofold
package com.yourself;
import java.util.Objects;
public class NonNull {
// }
/**
* This method requires a non null String
* A NullPointerException will be thrown if this is not the case. However, take a look at the line where the NPE would occur.
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

If you are a Guava user, you will have recognised this behaviour in Guava's Preconditions.checkNotNull.