Unclear inspection warning "NullableProblems" in IntelliJ

traveh picture traveh · Jul 6, 2015 · Viewed 12.8k times · Source

Why am I getting a warning from the "NullableProblems" inspection in IntelliJ on this:

public class Test implements Comparable<Test> {
    @Override
    public int compareTo(Test o) {
        return 0;
    }
}

I'm using IntelliJ 14.1.4 and compiling with Java 1.7

Screenshot:

enter image description here

Adding @NotNull before the argument doesn't help:

enter image description here

Answer

Darek Kay picture Darek Kay · Jul 6, 2015

From Comparable.compareTo:

@throws NullPointerException if the specified object is null

So IntelliJ knows, that the object should not be null and adds a @NotNull annotation automatically:

IntelliJ IDEA will look carefully at SDK and libraries bytecode and will infer these annotations automatically so that they can later be used to analyze source code to spot places where you overlooked null.

Your overriden method doesn't include this annotation, so it overrides this behavior making the parameter nullable - against the contract of the Comparable interface.

You can solve this by adding @NotNull before the parameter.

You can also disable this inspection by pressing Alt + Enter, selecting the warning in the popup menu and selecting Disable inspection in the sub-menu.

Check out the Web Help and this thread for more information about @NotNull / @NonNull annotations.