Fields, parameters and return values marked @NotNull
, @NonNull
, or @Nonnull
are assumed to have non-null
values and are not typically null-checked before use. Therefore setting one of these values to null
, or failing to set such a class field
in a constructor, could cause NullPointerException
s at runtime.
Noncompliant code example
public class MainClass {
@Nonnull
private String primary;
private String secondary;
public MainClass(String color) {
if (color != null) {
secondary = null;
}
primary = color; // Noncompliant; "primary" is Nonnull but could be set to null here
}
public MainClass() { // Noncompliant; "primary" is Nonnull but is not initialized
}
@Nonnull
public String indirectMix() {
String mix = null;
return mix; // Noncompliant; return value is Nonnull, but null is returned.
}