34

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

3

3 Answers 3

25

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.

9
  • 8
    Hmm, now I understand where the original warning comes from (checking the super class was the first thing I checked), but when I added @NotNull it didn't clear the warning... (updated original question with screenshot).
    – traveh
    Commented Jul 6, 2015 at 10:03
  • 3
    You are using the "wrong" @NotNull annotation. There are several NotNull/NonNull annotations out there, non of them included in the JDK. You will need to include and use one of those. Also check out the Web Help.
    – Darek Kay
    Commented Jul 6, 2015 at 10:09
  • 2
    Ha! Strange, when I added the @NotNull the import is what IntelliJ added automatically (after alt-enter I mean). I looked at the other question you linked to and I assume that com.intellij.annotations.NotNull is the correct annotation, but it seems that IntelliJ can't find the class...
    – traveh
    Commented Jul 6, 2015 at 10:18
  • 1
    You need to include the jar file containing the annotations. You can download (or include via Maven) IntelliJ annotations here. Another common alternative is javax.annotation.Nonnull (not NotNull), which you can find here. Both are recognized by IntelliJ.
    – Darek Kay
    Commented Jul 6, 2015 at 11:32
  • 1
    You have the control over your classpath. Unless a Jar is added to your classpath, an IDE won't provide any autocompletion. One reason is performance - imagine IntelliJ showing all possible classes, even if they are not included in your project.
    – Darek Kay
    Commented Jul 6, 2015 at 11:45
14

This can be globally configured in IntelliJ IDEA easily and for me personally is the recommended way. If you want you can add your own annotations.
i.e. javax.validation.constraints.NotNull

Path to the setting:
Settings > Editor > Inspections > @NotNull/@Nullable problems > Configure annotations

Some screenshots: enter image description here enter image description here

-1

Its because that you are overriding a method that does not have a @NotNull annotation.

IntelliJ IDEA warns you if the overriding method does not have a @NotNull annotation.

1
  • 1
    Nope. Check the interface.
    – traveh
    Commented Jul 6, 2015 at 10:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.