future is accessed as if it is non-null, but is marked as @Nullable!
48 /** Returns the current value or null if either not done or failed. */
49 @SuppressWarnings("NullAway")
50 static @Nullable <V> V getIfReady(@Nullable CompletableFuture<V> future) {
51 return isReady(future) ? future.join() : null; 52 }
53
54 /** Returns the value when completed successfully or null if failed. */
Description
This parameter is always used as if it is non-null, but the parameter may be null when the usage occurs.
Bad Practice
public void doWork(MyClass myClass) {
// ...
// Dereference without a null check.
myClass.myField = "value";
// ...
}
// ...
MyClass nullable;
// ...
if (condition) nullable = new MyClass();
else nullable = null;
// ...
doWork(nullable); // If nullable is null, we will get an NPE
Recommended
If you require that the parameter should never be null, consider changing the annotation to @NonNull
or an equivalent annotation to indicate that the method expects the value to be non-null.
If the variable is likely to be null, consider performing a null check before using the variable.