-1

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.

Gen
  • 2,400
  • 4
  • 24
  • 46
  • 3
    The length of an array can never be negative. `noofRecord < 0` is completely pointless – QBrute Aug 05 '21 at 07:58
  • Okay, Thank you, I will remove this line – Gen Aug 05 '21 at 07:59
  • 1
    The number zero doesn't belong to negetative numbers. The result of length is equal or greater than zero. So it makes no sense to check on negative numbers. – Reporter Aug 05 '21 at 07:59
  • @QBrute I think the first line counts the number of charaters from value of element `intarray[0]`. – Reporter Aug 05 '21 at 08:13
  • @Reporter `intarray` is probably a 2D array, so `intarray[0]` is a reguilar array which has the property `length`. Other than that I can only speculate, because there's not enough information about how `intarray` is actually defined. – QBrute Aug 05 '21 at 08:15
  • 2
    @QBrute I have updated the question with required info – Gen Aug 05 '21 at 08:22

1 Answers1

0

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.

Hulk
  • 6,399
  • 1
  • 30
  • 52