{"record":{"id":"161c793ed7407ee7","repo":"TheAlgorithms/Java","slug":"array-cannot-be-null","errorCode":null,"errorMessage":"Array cannot be null","messagePattern":"Array cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/others/InsertDeleteInArray.java","lineNumber":57,"sourceCode":"    /**\n     * Inserts an element at the specified position in the array.\n     * <p>\n     * Creates a new array with size = original array size + 1.\n     * Elements at positions &lt;= insertPos retain their positions,\n     * while elements at positions &gt; insertPos are shifted right by one position.\n     * </p>\n     *\n     * @param array    the original array\n     * @param element  the element to be inserted\n     * @param position the index at which the element should be inserted (0-based)\n     * @return a new array with the element inserted at the specified position\n     * @throws IllegalArgumentException if position is negative or greater than\n     *                                  array length\n     * @throws IllegalArgumentException if array is null\n     */\n    public static int[] insertElement(int[] array, int element, int position) {\n        if (array == null) {\n            throw new IllegalArgumentException(\"Array cannot be null\");\n        }\n        if (position < 0 || position > array.length) {\n            throw new IllegalArgumentException(\"Position must be between 0 and \" + array.length);\n        }\n\n        int[] newArray = new int[array.length + 1];\n\n        // Copy elements before insertion position\n        System.arraycopy(array, 0, newArray, 0, position);\n\n        // Insert the new element\n        newArray[position] = element;\n\n        // Copy remaining elements after insertion position\n        System.arraycopy(array, position, newArray, position + 1, array.length - position);\n\n        return newArray;\n    }","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/others/InsertDeleteInArray.java#L39-L75","documentation":"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).","triggerScenarios":"Calling insertElement(null, element, position), or passing an int[] that came from a method returning null on empty/error.","commonSituations":"Uninitialized array field, a loader returning null instead of an empty array, or a refactor that dropped the assignment.","solutions":["Ensure the array is non-null before calling (instantiate to an empty array if empty is valid).","Fix upstream methods to return empty arrays instead of null.","Add a null guard at the call site that supplies an empty array or fails with context."],"exampleFix":"// before\nint[] base = loadArray(); // may be null\nint[] updated = InsertDeleteInArray.insertElement(base, v, pos);\n\n// after\nint[] base = loadArray();\nif (base == null) base = new int[0];\nint[] updated = InsertDeleteInArray.insertElement(base, v, pos);","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(array, \"array\");\nint[] updated = InsertDeleteInArray.insertElement(array, element, position);","typeGuard":"static boolean isInsertable(int[] a, int position) {\n    return a != null && position >= 0 && position <= a.length;\n}","tryCatchPattern":null,"preventionTips":["Make array loaders return empty arrays, not null.","Add Objects.requireNonNull at the trust boundary."],"tags":["array","null-check","precondition","insert"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}