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

traveh
  • 2,700
  • 3
  • 27
  • 44
  • Read everything about ``@Nullable`` and ``@NotNull`` features [here](https://www.jetbrains.com/idea/documentation/howto.html) – Binkan Salaryman Jul 06 '15 at 09:47
  • Duplicate: http://stackoverflow.com/questions/24728627/meaning-of-android-studio-error-not-annotated-parameter-overrides-nonnull-para – davidgiga1993 Jul 06 '15 at 09:49
  • 5
    @davidgiga1993 Not a duplicate. I'm overriding a method that doesn't have the `@NotNull` annotation. – traveh Jul 06 '15 at 09:59

3 Answers3

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.

Community
  • 1
  • 1
Darek Kay
  • 15,827
  • 7
  • 64
  • 61
  • 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 Jul 06 '15 at 10:03
  • 3
    You are using the "wrong" `@NotNull` annotation. There are [several NotNull/NonNull annotations](http://stackoverflow.com/questions/4963300/which-notnull-java-annotation-should-i-use) 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](https://www.jetbrains.com/idea/documentation/howto.html). – Darek Kay Jul 06 '15 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 Jul 06 '15 at 10:18
  • I will post a new question about that. – traveh Jul 06 '15 at 10:21
  • http://stackoverflow.com/questions/31243474/intellij-suggests-wrong-notnull-annotation – traveh Jul 06 '15 at 10:27
  • 1
    You need to include the jar file containing the annotations. You can download (or include via Maven) IntelliJ annotations [here](http://mvnrepository.com/artifact/com.intellij/annotations/12.0). Another common alternative is `javax.annotation.Nonnull` (not NotNull), which you can find [here](http://mvnrepository.com/artifact/javax.annotation/jsr250-api/1.0). Both are recognized by IntelliJ. – Darek Kay Jul 06 '15 at 11:32
  • Yeah, I figured out this solves it (see the other question I posted), but I don't understand why IntelliJ originally suggests the wrong import and only when I add the jar it gets it right. Is that normal? – traveh Jul 06 '15 at 11:35
  • 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 Jul 06 '15 at 11:45
  • Aha, I guess that makes sense. I was worried there's something wrong with my IntelliJ installation. Thanks :) – traveh Jul 06 '15 at 11:48
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

magiccrafter
  • 5,175
  • 1
  • 56
  • 50
-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.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
Jeeppp
  • 1,553
  • 3
  • 17
  • 39