Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: condensed some text

...

This compliant solution eliminates the null pointer dereference by adding an explicit check.

Code Block
bgColor#ccccff
public static int cardinality(Object obj, final Collection col) {
  int count = 0;
  if (col == null) {
    return count;
  }
  Iterator it = col.iterator();
  while (it.hasNext()) {
    Object elt = it.next();
    if ((null == obj && null == elt) ||
        (null != obj && obj.equals(elt))) {
      count++;
    }
  }
  return count;
}

...

Noncompliant Code Example

...