null
twice JAVA-W1068There 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.
List<String> strings = getStrings();
Objects.requireNonNull(strings);
// ...a bunch of statements that don't modify the variable `strings`.
Objects.requireNonNull(strings); // Redundant check.
Consider removing redundant null checks.
List<String> strings = getStrings();
Objects.requireNonNull(strings);