javafindbugscoverity

INT_BAD_COMPARISON_WITH_NONNEGATIVE_VALUE findbugs


I am getting the error in my coverity.

Suspicious integer expression (FB.INT_BAD_COMPARISON_WITH_NONNEGATIVE_VALUE)

  1. defect: Bad comparison of nonnegative value with 0.

Code where the issue raised

                  int [][] intarray = DB Call;
                  int noofRecord = intarray[0].length;
                    if(noofRecord < 0) {
                      //some stuf
                  }

How can I resolve the above error.


Solution

  • You get this hint from findbugs because this analyzer detects that the condition can never be true.

    Array length is guranteed by the language specification to be non-negative (JLS 16 10.7. Array Members):

    The members of an array type are all of the following:

    • The public final field length, which contains the number of components of the array. length may be positive or zero.

    [...]

    Therefore, the code inside the if-block can never be executed and is effectively dead code.