Problem
User has the following code in which he is explicitly testing for null:
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class NullInspectionSample {
interface Message {
@Nullable Member getMember();
}
interface Member {
@Nullable String getNickname();
@NotNull String getEffectiveName();
}
interface FooEvent {
@Nullable Member getMember();
}
void testInspection(FooEvent event) {
String characterName = event.getMember().getNickname() != null // Method invocation 'getNickname' may produce 'NullPointerException'
? event.getMember().getNickname()
: event.getMember().getEffectiveName();
System.out.println(characterName);
}
}
But IntelliJ IDEA reports the following problem:
Method invocation '...' may produce 'NullPointerException'
Why is there a warning about the @Nullable condition?
Explanation
IntelliJ IDEA uses data flow analysis to identify code constructs that may violate nullability contracts, potentially throw exceptions, or are redundant. It allows to trace all the possible data transformations without actually running the program.
This analysis is designed to be quick and provide on-the-fly suggestions, and as a result, it does not perform complex logic on code analysis.
Solution
This warning is not an error and does not prevent the code from running.
If you prefer to hide it, you can do it like this:
-
Go to Settings | Editor | Inspections
-
Search for the inspection named "Nullability and data flow problems"
-
In its settings on the right, scroll down to the Options section and disable the option "Report problems that happen only on some code paths"
⚠️ If you have a problem with a specific code construct and believe that the IDE should exclude it from analysis, or if you want to suggest other improvements to the logic, you are welcome to submit a new feature request. We will make sure to address it.
Comments
0 comments
Please sign in to leave a comment.