kgf3JfUtW
kgf3JfUtW

Reputation: 14918

Sonar: Null pointers should not be dereferenced

False-positives are pointed out in other SO posts (1 & 2), where nulls are involved across functions and procedures.

I feel my situation is not as complicated.

    // A and B are both Double[]

    if (A.length == 1 || (B != null && B.length == 1)) {
        Double vol;
        if (A.length == 1) {
            vol = A[0];
        } else {
            vol = B[0]; // Sonar Blocker: Null pointers should not be deferenced
        }
    }

I could've fixed this by changing else to else if (B != null && B.length == 1), but doing so rewards me with warning "condition(B != null && B.length == 1) is always true".

Did I miss something here?

Upvotes: 2

Views: 3036

Answers (1)

Nabin Bhandari
Nabin Bhandari

Reputation: 16379

Your code is ok.

When B is null, The control reaches inside the main if block only when length of A is 1. But when length of A is 1, the control never reaches the else block.

PS: I also tried the code in android studio which uses Lint and got no warnings.

Upvotes: 1

Related Questions