{"record":{"id":"1a7c668969eb9880","repo":"TheAlgorithms/Java","slug":"ipv4-address-is-empty","errorCode":null,"errorMessage":"IPv4 address is empty.","messagePattern":"IPv4 address is empty\\.","errorType":"validation","errorClass":"UnknownHostException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/IPv6Converter.java","lineNumber":31,"sourceCode":" *\n * @author Hardvan\n */\npublic final class IPv6Converter {\n    private IPv6Converter() {\n    }\n\n    /**\n     * Converts an IPv4 address (e.g., \"192.0.2.128\") to an IPv6-mapped IPv6 address.\n     * Example: IPv4 \"192.0.2.128\" -> IPv6 \"::ffff:192.0.2.128\"\n     *\n     * @param ipv4Address The IPv4 address in string format.\n     * @return The corresponding IPv6-mapped IPv6 address.\n     * @throws UnknownHostException If the IPv4 address is invalid.\n     * @throws IllegalArgumentException If the IPv6 address is not a mapped IPv4 address.\n     */\n    public static String ipv4ToIpv6(String ipv4Address) throws UnknownHostException {\n        if (ipv4Address == null || ipv4Address.isEmpty()) {\n            throw new UnknownHostException(\"IPv4 address is empty.\");\n        }\n\n        InetAddress ipv4 = InetAddress.getByName(ipv4Address);\n        byte[] ipv4Bytes = ipv4.getAddress();\n\n        // Create IPv6-mapped IPv6 address (starts with ::ffff:)\n        byte[] ipv6Bytes = new byte[16];\n        ipv6Bytes[10] = (byte) 0xff;\n        ipv6Bytes[11] = (byte) 0xff;\n        System.arraycopy(ipv4Bytes, 0, ipv6Bytes, 12, 4);\n\n        // Manually format to \"::ffff:x.x.x.x\" format\n        StringBuilder ipv6String = new StringBuilder(\"::ffff:\");\n        for (int i = 12; i < 16; i++) {\n            ipv6String.append(ipv6Bytes[i] & 0xFF);\n            if (i < 15) {\n                ipv6String.append('.');\n            }","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/IPv6Converter.java#L13-L49","documentation":"Thrown by IPv6Converter.ipv4ToIpv6 as an UnknownHostException when ipv4Address is null or an empty string. Note: the Javadoc mentions @throws IllegalArgumentException, but the actual code throws UnknownHostException, so callers must catch UnknownHostException specifically. The guard fires before any DNS resolution attempt.","triggerScenarios":"Calling ipv4ToIpv6(null) or ipv4ToIpv6(\"\"). Passing a String variable that was initialized to null or not populated from a config/API response. Supplying an empty field from a parsed CSV or properties file.","commonSituations":"A network configuration key is missing from a properties file, yielding null or empty. A JSON field is absent and mapped to null by the deserializer. The IPv4 string is conditionally set but the condition was not met.","solutions":["Check for null or empty before calling ipv4ToIpv6 and provide a meaningful error or default.","Use Objects.requireNonNull(ipv4Address, \"ipv4Address\") if null should be a programming error.","If reading from a config file, validate the property is present and non-empty at startup."],"exampleFix":"// before\nString ipv6 = IPv6Converter.ipv4ToIpv6(ipv4Str); // ipv4Str may be null or empty\n\n// after\nif (ipv4Str == null || ipv4Str.isEmpty()) {\n    throw new IllegalArgumentException(\"IPv4 address must not be null or empty\");\n}\nString ipv6 = IPv6Converter.ipv4ToIpv6(ipv4Str);","handlingStrategy":"validation","validationCode":"if (ipv4Address == null || ipv4Address.isEmpty()) {\n    throw new IllegalArgumentException(\"IPv4 address must not be null or empty\");\n}\nString result = IPv6Converter.ipv4ToIpv6(ipv4Address);","typeGuard":"static boolean isValidIpv4String(String addr) {\n    return addr != null && !addr.isEmpty() && addr.matches(\"^\\\\d{1,3}(\\\\.\\\\d{1,3}){3}$\");\n}","tryCatchPattern":"try {\n    String ipv6 = IPv6Converter.ipv4ToIpv6(ipv4Address);\n} catch (UnknownHostException e) {\n    // null, empty, or unresolvable address; handle gracefully\n    logger.warn(\"Invalid IPv4 address: {}\", ipv4Address);\n}","preventionTips":["Note that the method throws UnknownHostException (not IllegalArgumentException) for null/empty — catch the correct exception type.","Validate address strings at the network boundary before conversion.","Use InetAddress.getByName only for trusted input, as it performs DNS resolution."],"tags":["network","ipv4","ipv6","input-validation","null-check"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}