I commonly come across the following IntelliJ inspection
private boolean bar() {
return foo().contains("foo"); // Method invocation 'contains' may produce 'java.lang.NullPointerException'
}
private String foo() {
return null;
}
The inspections seem fine to me, but one of (or often times the only) suggested fix from IntelliJ is this:
private boolean bar() {
return Objects.requireNonNull(foo()).contains("foo");
}
The warning is then gone. But I don't understand how this helps at all? requireNonNull
will just throw the same NullPointerException
that would have been thrown anyhow when .contains
was invoked on null
.
Usually, IntelliJ makes meaningful suggestions, and this is a common one, so am I missing the point here?