TheAlgorithms/Java · error · IllegalArgumentException

Array cannot be null

Error message

Array cannot be null

What it means

Thrown by InsertDeleteInArray.insertElement when the input array is null. The method builds a new array of length array.length+1 and uses System.arraycopy from the source, so a null array would NPE. The guard rejects null explicitly with a clear message. This is the first of two precondition checks (null array, then position bounds).

Source

Thrown at src/main/java/com/thealgorithms/others/InsertDeleteInArray.java:57

    /**
     * Inserts an element at the specified position in the array.
     * <p>
     * Creates a new array with size = original array size + 1.
     * Elements at positions &lt;= insertPos retain their positions,
     * while elements at positions &gt; insertPos are shifted right by one position.
     * </p>
     *
     * @param array    the original array
     * @param element  the element to be inserted
     * @param position the index at which the element should be inserted (0-based)
     * @return a new array with the element inserted at the specified position
     * @throws IllegalArgumentException if position is negative or greater than
     *                                  array length
     * @throws IllegalArgumentException if array is null
     */
    public static int[] insertElement(int[] array, int element, int position) {
        if (array == null) {
            throw new IllegalArgumentException("Array cannot be null");
        }
        if (position < 0 || position > array.length) {
            throw new IllegalArgumentException("Position must be between 0 and " + array.length);
        }

        int[] newArray = new int[array.length + 1];

        // Copy elements before insertion position
        System.arraycopy(array, 0, newArray, 0, position);

        // Insert the new element
        newArray[position] = element;

        // Copy remaining elements after insertion position
        System.arraycopy(array, position, newArray, position + 1, array.length - position);

        return newArray;
    }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure the array is non-null before calling (instantiate to an empty array if empty is valid).
  2. Fix upstream methods to return empty arrays instead of null.
  3. Add a null guard at the call site that supplies an empty array or fails with context.

Example fix

// before
int[] base = loadArray(); // may be null
int[] updated = InsertDeleteInArray.insertElement(base, v, pos);

// after
int[] base = loadArray();
if (base == null) base = new int[0];
int[] updated = InsertDeleteInArray.insertElement(base, v, pos);
Defensive patterns

Strategy: validation

Validate before calling

Objects.requireNonNull(array, "array");
int[] updated = InsertDeleteInArray.insertElement(array, element, position);

Type guard

static boolean isInsertable(int[] a, int position) {
    return a != null && position >= 0 && position <= a.length;
}

Prevention

When it happens

Trigger: Calling insertElement(null, element, position), or passing an int[] that came from a method returning null on empty/error.

Common situations: Uninitialized array field, a loader returning null instead of an empty array, or a refactor that dropped the assignment.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/161c793ed7407ee7. Report an issue: GitHub.