...
This code can fail as a result of the same errors it is trying to prevent. When array is NULL or i is not a valid index, the reference to to array and array[i] will cause either a NullPointerException or an ArrayIndexOutOfBoundsException to be thrown. This happens because the & operator fails to prevent evaluation of its right operand even when evaluation of its left operand proves that the right operand is invalidinconsequential.
Compliant Solution (Use &&)
...
| Code Block | ||
|---|---|---|
| ||
int array[]; // May be null
int i; // May be a valid index for array
if (array != null) {
if (i >= 0 && i < array.length) {
if (array[i] !>= -10) {
// Use array
} else {
// Handle error
}
} else {
// Handle error
}
} else {
// Handle error
}
|
...