{"record":{"id":"bdacd9b863452ffb","repo":"TheAlgorithms/Java","slug":"input-array-must-not-be-null","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/misc/ShuffleArray.java","lineNumber":32,"sourceCode":" *\n * This class provides a static method to shuffle an array in place.\n *\n * @author Rashi Dashore (https://github.com/rashi07dashore)\n */\npublic final class ShuffleArray {\n\n    private ShuffleArray() {\n    }\n\n    /**\n     * Shuffles the provided array in-place using the Fisher–Yates algorithm.\n     *\n     * @param arr the array to shuffle; must not be {@code null}\n     * @throws IllegalArgumentException if the input array is {@code null}\n     */\n    public static void shuffle(int[] arr) {\n        if (arr == null) {\n            throw new IllegalArgumentException(\"Input array must not be null\");\n        }\n\n        Random random = new Random();\n        for (int i = arr.length - 1; i > 0; i--) {\n            int j = random.nextInt(i + 1);\n            swap(arr, i, j);\n        }\n    }\n\n    /**\n     * Swaps two elements in an array.\n     *\n     * @param arr the array\n     * @param i   index of first element\n     * @param j   index of second element\n     */\n    private static void swap(int[] arr, int i, int j) {\n        if (i != j) {","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/misc/ShuffleArray.java#L14-L50","documentation":"Thrown by ShuffleArray.shuffle when the input int[] arr is null. shuffle performs an in-place Fisher–Yates shuffle indexing arr.length-1 down to 1, so a null array would NPE inside the loop. The guard rejects null explicitly with a clear message. An empty or single-element array is allowed (the loop simply does nothing).","triggerScenarios":"Calling ShuffleArray.shuffle(null), or passing an int[] variable that was never assigned / returned null from a loader. Common when shuffling a list converted via stream().mapToInt(...).toArray() on an empty/null source that yielded null.","commonSituations":"Uninitialized array field, a helper that returns null on empty input instead of an empty array, or a refactor that dropped the assignment.","solutions":["Ensure the array is non-null before shuffling (instantiate to an empty array if empty).","Fix upstream methods to return empty arrays rather than null.","Add a null guard at the call site that skips shuffling for null."],"exampleFix":"// before\nint[] deck = loadDeck(); // may be null\nShuffleArray.shuffle(deck);\n\n// after\nint[] deck = loadDeck();\nif (deck == null) deck = new int[0];\nif (deck.length > 1) ShuffleArray.shuffle(deck);","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(arr, \"arr\");\nif (arr.length > 1) ShuffleArray.shuffle(arr);","typeGuard":"static boolean isShuffleable(int[] a) {\n    return a != null && a.length > 1;\n}","tryCatchPattern":null,"preventionTips":["Have array-returning helpers return empty arrays, not null.","Skip shuffling for arrays of length 0 or 1 (no-op)."],"tags":["array","null-check","precondition","shuffle","fisher-yates"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}