Java

Java

Made by DeepSource

Variable is checked for null twice JAVA-W1068

Anti-pattern
Major

There is no point in repetitively checking if a variable is null if the variable hasn't been written between those checks.

Such checks are useless and can only lead to confusion. Therefore, consecutive null checks using methods such as checkNotNull(), verifyNotNull(), and requireNonNull() should be avoided.

Bad Practice

List<String> strings = getStrings();
Objects.requireNonNull(strings);
// ...a bunch of statements that don't modify the variable `strings`.
Objects.requireNonNull(strings); // Redundant check.

Recommended

Consider removing redundant null checks.

List<String> strings = getStrings();
Objects.requireNonNull(strings);

References