Java

Java

Made by DeepSource

Methods annotated as non-nullable should not return null values JAVA-E1095

Bug risk
Critical

A method that is marked with annotations such as @Nonnull should not return explicit null values.

Returning null when the method is explicitly marked as non-null is a bad practice, since it defeats the purpose of the method being annotated in the first place.

Bad Practice

@Nonnull
String someMethod() {
    // ...

    if (someCondition) {
        return null; // Not right!
    }

    // ...
    return value;
}

Recommended

Avoid returning null if the method is marked as not returning null.