TheAlgorithms/Java · error · IllegalArgumentException
The input array cannot be null
Error message
The input array cannot be null
What it means
Thrown by BitonicSort.sort(T[]) when array == null. Bitonic sort pads the array to the next power of two and then compares elements, both of which dereference the array reference. The guard converts a latent NullPointerException into a descriptive IllegalArgumentException before any processing.
Source
Thrown at src/main/java/com/thealgorithms/sorts/BitonicSort.java:25
* BitonicSort class implements the SortAlgorithm interface using the bitonic sort technique.
*/
public class BitonicSort implements SortAlgorithm {
private enum Direction {
DESCENDING,
ASCENDING,
}
/**
* Sorts the given array using the Bitonic Sort algorithm.
*
* @param <T> the type of elements in the array, which must implement the Comparable interface
* @param array the array to be sorted
* @return the sorted array
*/
@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
if (array == null) {
throw new IllegalArgumentException("The input array cannot be null");
}
if (array.length == 0) {
return array;
}
final int paddedSize = nextPowerOfTwo(array.length);
T[] paddedArray = Arrays.copyOf(array, paddedSize);
// Fill the padded part with a maximum value
final T maxValue = max(array);
Arrays.fill(paddedArray, array.length, paddedSize, maxValue);
bitonicSort(paddedArray, 0, paddedSize, Direction.ASCENDING);
return Arrays.copyOf(paddedArray, array.length);
}
private <T extends Comparable<T>> void bitonicSort(final T[] array, final int low, final int cnt, final Direction direction) {
if (cnt > 1) {View on GitHub (pinned to fdfb9a395b)
Solutions
- Null-check the array before calling sort() and return an empty array or skip.
- Initialize collections/arrays to empty rather than null.
- Annotate the parameter @NonNull and enable static analysis.
Example fix
// before
T[] sorted = new BitonicSort().sort(arr);
// after
if (arr == null) throw new IllegalArgumentException("arr must not be null");
T[] sorted = new BitonicSort().sort(arr); Defensive patterns
Strategy: validation
Validate before calling
if (array == null) throw new IllegalArgumentException("array must not be null"); Type guard
public static <T> boolean isSortable(T[] array) { return array != null; } Prevention
- Initialize array fields to empty arrays rather than null.
- Null-check at the API boundary before sorting.
- Annotate with @NonNull and enable static null analysis.
When it happens
Trigger: Calling sort(null); passing a reference initialized to null; forwarding a value from a Map.get() that returned null.
Common situations: Uninitialized fields; data loaders returning null on missing input; null used as a sentinel for 'no data'.
Related errors
- Input array must not be null.
- Key must not be null.
- Array cannot be null
- IPv4 address is empty.
- Input cannot be null or empty
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/753c180590b8a327.
Report an issue: GitHub.