You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This works great. However, when using a value returned by a method annotated with @Nullable, IntelliJ warns me: Method invocation 'xxx' may produce 'NullPointerException'.
Eg:
// a method that returns a @Nullable objectpublic@NullableMyObjectdoSomething() {
if(...){
returnnewMyObject();
}else{
returnnull;
}
}
// extension method which accepts a @Nullable objectpublicstaticbooleanvalidate(@This@NullableMyObjectthiz, Predicate<MyObject> predicate) {
returnthis != null && predicate.test(this);
}
// doSomething().validate(<predicate>); // --> Method invocation 'validate' may produce 'NullPointerException'
This is clearly not the case: Even if the value is null, it is still correctly handled, and no NPE is thrown.
To Reproduce
Steps to reproduce the behavior:
Annotate the return value (eg MyObject) of a method with @Nullable
Write an extension method for the return type (MyObject), which accepts a @Nullable MyObject
Call the method of the extension on a @Nullable value.
Expected behavior
IntelliJ should not warn me about a potential NPE
Desktop (please complete the following information):
OS Type & Version: Windows 10 22H2
Java/JDK version: openjdk 21.0.1
IDE version (IntelliJ IDEA or Android Studio): IntelliJ IDEA 2023.2.5
The text was updated successfully, but these errors were encountered:
EotT123
changed the title
Wrong warning: Method invocation may produce 'NullPointerException'
Incorrect IntelliJ warning: Method invocation may produce 'NullPointerException'
Nov 24, 2023
While technically true, this would make extension methods behave different from normal methods when it comes to nullability.
I think it would be better to invest into null-safe operators like ?. for null-safe method calls or ?? to provide a default value when the object is null.
In Kotlin you'd have to mark a receiver specifically to be nullable, if you want to be allowed to call an extension method on a variable that contains the null value. This also adds clarity, since now you know if it's expected if you're allowed to use null in the call or if you'd need to use the ?. operator to make it a null-safe call.
As an example where there's a difference between nullable extension methods called with ?. and .:
Describe the bug
Extension methods are a great way to deal with nullness, eg see the example here:
This works great. However, when using a value returned by a method annotated with
@Nullable
, IntelliJ warns me:Method invocation 'xxx' may produce 'NullPointerException'
.Eg:
This is clearly not the case: Even if the value is null, it is still correctly handled, and no NPE is thrown.
To Reproduce
Steps to reproduce the behavior:
MyObject
) of a method with@Nullable
MyObject
), which accepts a@Nullable MyObject
@Nullable
value.Expected behavior
IntelliJ should not warn me about a potential NPE
Desktop (please complete the following information):
The text was updated successfully, but these errors were encountered: