Java

Java

Made by DeepSource

Possibly null fields should not be synchronized on JAVA-E1060

Bug risk
Critical
sans top 25 cwe-476 cwe-667

There is a null check for a field which is being synchronized on. Since the field is synchronized on, it is unlikely that it will be null. If it is null and then synchronized on, a NullPointerException will be thrown on synchronization and the check would be pointless.

Bad Practice

// may throw NPE.
synchronize(possiblyNull) {
    if (possiblyNull != null) {
        // ...
    }
}

Recommended

synchronize(nonNull) {
    if (possiblyNull != null) {
        // ...
    }
}

Avoid structuring your code in a way that allows for nullable values to be used in a non-null context.

References