OutOfBoundsElementsAreNull
DRAFT

Misconception:

An access of an array element that is out of bounds (e.g., a[a.length]) produces null to indicate that the element does not exist.

Incorrect

Out-of-bounds array elements are null

Correct

Out-of-bounds array elements cannot be accessed

Correction
Here is what's right.

Accessing an out-of-bounds element of an array throws an ArrayIndexOutOfBoundsException.

For example, in the following code, the while loop condition is not a correct way to finish the loop. a[i] will not produce null when i >= a.length; instead the expression will throw an ArrayIndexOutOfBoundsException.

public int m(int[] a) {
  int i = 0;
  int sum = 0;
  while (a[i] != null) { // error
    sum += a[i];
    i++;
  }
  return sum;
}

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.