{"record":{"id":"edbc65b86db8ea0e","repo":"TheAlgorithms/Java","slug":"inputs-cannot-be-null","errorCode":null,"errorMessage":"Inputs cannot be null.","messagePattern":"Inputs cannot be null\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java","lineNumber":110,"sourceCode":"        // Compute Public Key y = g^x mod p\n        y = g.modPow(x, p);\n\n        return new KeyPair(p, g, y, x);\n    }\n\n    /**\n     * Encrypts a message using the public key.\n     *\n     * @param message The message converted to BigInteger.\n     * @param p       The prime modulus.\n     * @param g       The generator.\n     * @param y       The public key component.\n     * @return The CipherText pair (a, b).\n     * @throws IllegalArgumentException if inputs are null, negative, or message >= p.\n     */\n    public static CipherText encrypt(BigInteger message, BigInteger p, BigInteger g, BigInteger y) {\n        if (message == null || p == null || g == null || y == null) {\n            throw new IllegalArgumentException(\"Inputs cannot be null.\");\n        }\n        if (message.compareTo(BigInteger.ZERO) < 0) {\n            throw new IllegalArgumentException(\"Message must be non-negative.\");\n        }\n        if (message.compareTo(p) >= 0) {\n            throw new IllegalArgumentException(\"Message must be smaller than the prime modulus p.\");\n        }\n\n        BigInteger k;\n        BigInteger pMinus1 = p.subtract(BigInteger.ONE);\n\n        // Select ephemeral key k such that 1 < k < p-1 and gcd(k, p-1) = 1\n        do {\n            k = new BigInteger(p.bitLength(), RANDOM);\n        } while (k.compareTo(BigInteger.ONE) <= 0 || k.compareTo(pMinus1) >= 0 || !k.gcd(pMinus1).equals(BigInteger.ONE));\n\n        BigInteger a = g.modPow(k, p);\n        BigInteger b = y.modPow(k, p).multiply(message).mod(p);","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java#L92-L128","documentation":"Thrown by ElGamalCipher.encrypt(BigInteger, BigInteger, BigInteger, BigInteger) when any of message, p, g, or y is null. This is the first of three sequential guards in encrypt; it rejects null inputs before the value-range checks.","triggerScenarios":"Calling encrypt with any argument null. Subsequent guards then check message sign (error 32) and message < p (error 33).","commonSituations":"A key component (p, g, y) not yet generated; message not initialized; deserialization producing null fields.","solutions":["Ensure all four arguments are non-null before encrypting.","Generate keys via generateKeys first and pass its components directly.","Add null checks in your data-loading layer."],"exampleFix":"// before\nCipherText ct = ElGamalCipher.encrypt(msg, p, g, y);\n\n// after\nObjects.requireNonNull(message, \"message\");\nObjects.requireNonNull(p, \"p\");\nObjects.requireNonNull(g, \"g\");\nObjects.requireNonNull(y, \"y\");\nCipherText ct = ElGamalCipher.encrypt(message, p, g, y);","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(message, \"message\");\nObjects.requireNonNull(p, \"p\");\nObjects.requireNonNull(g, \"g\");\nObjects.requireNonNull(y, \"y\");\nCipherText ct = ElGamalCipher.encrypt(message, p, g, y);","typeGuard":"static boolean allNonNull(Object... objs) {\n    for (Object o : objs) if (o == null) return false;\n    return true;\n}","tryCatchPattern":"try {\n    CipherText ct = ElGamalCipher.encrypt(message, p, g, y);\n} catch (IllegalArgumentException e) {\n    // an input was null; ensure keys are generated/loaded\n}","preventionTips":["Generate keys via generateKeys first; pass its components directly.","Add null checks in the data-loading layer.","Keep key components together to avoid missing fields."],"tags":["cryptography","ciphers","elgamal","java","validation","null-safety","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}