{"record":{"id":"6d698ea095731a26","repo":"TheAlgorithms/Java","slug":"key-cannot-be-null-or-empty","errorCode":null,"errorMessage":"Key cannot be null or empty","messagePattern":"Key cannot be null or empty","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/ciphers/PermutationCipher.java","lineNumber":91,"sourceCode":"        // Process text in blocks of key length\r\n        for (int i = 0; i < ciphertext.length(); i += key.length) {\r\n            String block = ciphertext.substring(i, Math.min(i + key.length, ciphertext.length()));\r\n            decrypted.append(permuteBlock(block, inverseKey));\r\n        }\r\n\r\n        // Remove padding characters from the end\r\n        return removePadding(decrypted.toString());\r\n    }\r\n    /**\r\n     * Validates that the permutation key is valid.\r\n     * A valid key must contain all integers from 1 to n exactly once, where n is the key length.\r\n     *\r\n     * @param key the permutation key to validate\r\n     * @throws IllegalArgumentException if the key is invalid\r\n     */\r\n    private void validateKey(int[] key) {\r\n        if (key == null || key.length == 0) {\r\n            throw new IllegalArgumentException(\"Key cannot be null or empty\");\r\n        }\r\n\r\n        Set<Integer> keySet = new HashSet<>();\r\n        for (int position : key) {\r\n            if (position < 1 || position > key.length) {\r\n                throw new IllegalArgumentException(\"Key must contain integers from 1 to \" + key.length);\r\n            }\r\n            if (!keySet.add(position)) {\r\n                throw new IllegalArgumentException(\"Key must contain each position exactly once\");\r\n            }\r\n        }\r\n    }\r\n\r\n    /**\r\n     * Pads the text with padding characters to make its length divisible by the block size.\r\n     *\r\n     * @param text the text to pad\r\n     * @param blockSize the size of each block\r","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/ciphers/PermutationCipher.java#L73-L109","documentation":"PermutationCipher calls validateKey() at the entry of both encrypt() and decrypt(), and the first guard rejects a key that is null or has length zero. A transposition cipher rearranges characters within a block of size equal to the key length, so a zero-length key leaves the block size undefined and the operation cannot proceed.","triggerScenarios":"Calling cipher.encrypt(text, null), cipher.encrypt(text, new int[0]), cipher.decrypt(text, null), or cipher.decrypt(text, new int[0]) — any path where the int[] key argument is null or an empty array.","commonSituations":"Key deserialized from JSON or a config file that came back as null; key array allocated with a computed size that evaluated to 0; user-supplied key field left blank in a UI.","solutions":["Pass a non-empty permutation array such as new int[]{3, 1, 2} where every value is a 1-based position.","Null-check the key source (e.g. config/JSON) before calling encrypt or decrypt and surface a clear upstream error.","If the key is optional in your flow, short-circuit and skip the cipher call instead of passing null."],"exampleFix":"// before\ncipher.encrypt(text, keyFromConfig);\n\n// after\nif (keyFromConfig == null || keyFromConfig.length == 0) {\n    throw new IllegalStateException(\"Permutation key missing from config\");\n}\ncipher.encrypt(text, keyFromConfig);","handlingStrategy":"validation","validationCode":"if (key == null || key.length == 0) {\n    throw new IllegalArgumentException(\"Permutation key must be non-null and non-empty\");\n}\ncipher.encrypt(text, key);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Null-check the key source before invoking encrypt/decrypt.","Derive the key from a shuffled 1..n list so it is never empty by construction.","Add a unit test that asserts the cipher throws on a null/empty key rather than relying on runtime."],"tags":["cipher","permutation","validation","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}