简体   繁体   中英

Unclear inspection warning “NullableProblems” in IntelliJ

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:

在此输入图像描述

Adding @NotNull before the argument doesn't help:

在此输入图像描述

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.

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.
ie javax.validation.constraints.NotNull

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

Some screenshots: 在此输入图像描述 在此输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM