TheAlgorithms/Java · error · IllegalArgumentException
Key cannot be null or empty
Error message
Key cannot be null or empty
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/ciphers/PermutationCipher.java:91
// Process text in blocks of key length
for (int i = 0; i < ciphertext.length(); i += key.length) {
String block = ciphertext.substring(i, Math.min(i + key.length, ciphertext.length()));
decrypted.append(permuteBlock(block, inverseKey));
}
// Remove padding characters from the end
return removePadding(decrypted.toString());
}
/**
* Validates that the permutation key is valid.
* A valid key must contain all integers from 1 to n exactly once, where n is the key length.
*
* @param key the permutation key to validate
* @throws IllegalArgumentException if the key is invalid
*/
private void validateKey(int[] key) {
if (key == null || key.length == 0) {
throw new IllegalArgumentException("Key cannot be null or empty");
}
Set<Integer> keySet = new HashSet<>();
for (int position : key) {
if (position < 1 || position > key.length) {
throw new IllegalArgumentException("Key must contain integers from 1 to " + key.length);
}
if (!keySet.add(position)) {
throw new IllegalArgumentException("Key must contain each position exactly once");
}
}
}
/**
* Pads the text with padding characters to make its length divisible by the block size.
*
* @param text the text to pad
* @param blockSize the size of each block
View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
cipher.encrypt(text, keyFromConfig);
// after
if (keyFromConfig == null || keyFromConfig.length == 0) {
throw new IllegalStateException("Permutation key missing from config");
}
cipher.encrypt(text, keyFromConfig); Defensive patterns
Strategy: validation
Validate before calling
if (key == null || key.length == 0) {
throw new IllegalArgumentException("Permutation key must be non-null and non-empty");
}
cipher.encrypt(text, key); Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Key must contain integers from 1 to {}
- Key must contain each position exactly once
- Message is empty
- Key cannot be empty.
- Key must not be empty
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/6d698ea095731a26.
Report an issue: GitHub.