Arrays.sort
requires all of the array elements to be non-null. You want to sort only the non-null part, so replace Array.sort(data)
with Arrays.sort(data, 0, topIndex + 1)
.
Arrays.sort(Object[], int, int)
Do not modify compareTo
to allow a null argument as others have suggested, because the contract of Comparable
dictates that your implementation should throw NullPointerException
.