{"record":{"id":"6b7c27b693c332ca","repo":"TheAlgorithms/Java","slug":"input-array-must-not-be-null-6b7c27","errorCode":null,"errorMessage":"Input array must not be null.","messagePattern":"Input array must not be null\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/sorts/LibrarySort.java","lineNumber":36,"sourceCode":" */\npublic final class LibrarySort {\n\n    private static final int GAP_FACTOR = 2;\n\n    private LibrarySort() {\n        // Utility class\n    }\n\n    /**\n     * Sorts an array using the Library Sort algorithm.\n     *\n     * @param array the array to sort (must not be null)\n     * @return the sorted array\n     * @throws IllegalArgumentException if {@code array} is {@code null}\n     */\n    public static int[] sort(final int[] array) {\n        if (array == null) {\n            throw new IllegalArgumentException(\"Input array must not be null.\");\n        }\n        if (array.length <= 1) {\n            return array;\n        }\n\n        final int n = array.length;\n        final int capacity = GAP_FACTOR * n;\n        final int[] data = new int[capacity];\n        final boolean[] occupied = new boolean[capacity];\n\n        final int mid = capacity / 2;\n        data[mid] = array[0];\n        occupied[mid] = true;\n\n        int filled = 1;\n        int nextToInsert = 1;\n        int round = 0;\n        while (nextToInsert < n) {","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/sorts/LibrarySort.java#L18-L54","documentation":"Thrown by LibrarySort.sort(int[]) when array == null. Library sort inserts elements into a gap-based structure, which requires a concrete array reference. The guard converts a potential NullPointerException into a clear IllegalArgumentException before allocating the internal buffers.","triggerScenarios":"Calling sort(null); passing an uninitialized int[] field; forwarding a null from a parser or Map.get().","commonSituations":"Optional data left null; data source returning null for missing input; null used as an 'absent' marker.","solutions":["Null-check the array before calling sort() and return an empty array.","Initialize int[] fields to empty arrays rather than null.","Use Optional<int[]> or @NonNull annotations to make nullability explicit."],"exampleFix":"// before\nint[] sorted = LibrarySort.sort(arr);\n\n// after\nif (arr == null) return new int[0];\nint[] sorted = LibrarySort.sort(arr);","handlingStrategy":"validation","validationCode":"if (array == null) return new int[0];","typeGuard":"public static boolean isSortable(int[] array) { return array != null; }","tryCatchPattern":null,"preventionTips":["Initialize int[] fields to empty arrays rather than null.","Null-check at the API boundary before calling LibrarySort.sort.","Use Optional<int[]> or @NonNull to make nullability explicit."],"tags":["sorting","nullable","null-check","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}