TheAlgorithms/Java · error · UnknownHostException

IPv4 address is empty.

Error message

IPv4 address is empty.

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/conversions/IPv6Converter.java:31

 *
 * @author Hardvan
 */
public final class IPv6Converter {
    private IPv6Converter() {
    }

    /**
     * Converts an IPv4 address (e.g., "192.0.2.128") to an IPv6-mapped IPv6 address.
     * Example: IPv4 "192.0.2.128" -> IPv6 "::ffff:192.0.2.128"
     *
     * @param ipv4Address The IPv4 address in string format.
     * @return The corresponding IPv6-mapped IPv6 address.
     * @throws UnknownHostException If the IPv4 address is invalid.
     * @throws IllegalArgumentException If the IPv6 address is not a mapped IPv4 address.
     */
    public static String ipv4ToIpv6(String ipv4Address) throws UnknownHostException {
        if (ipv4Address == null || ipv4Address.isEmpty()) {
            throw new UnknownHostException("IPv4 address is empty.");
        }

        InetAddress ipv4 = InetAddress.getByName(ipv4Address);
        byte[] ipv4Bytes = ipv4.getAddress();

        // Create IPv6-mapped IPv6 address (starts with ::ffff:)
        byte[] ipv6Bytes = new byte[16];
        ipv6Bytes[10] = (byte) 0xff;
        ipv6Bytes[11] = (byte) 0xff;
        System.arraycopy(ipv4Bytes, 0, ipv6Bytes, 12, 4);

        // Manually format to "::ffff:x.x.x.x" format
        StringBuilder ipv6String = new StringBuilder("::ffff:");
        for (int i = 12; i < 16; i++) {
            ipv6String.append(ipv6Bytes[i] & 0xFF);
            if (i < 15) {
                ipv6String.append('.');
            }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Check for null or empty before calling ipv4ToIpv6 and provide a meaningful error or default.
  2. Use Objects.requireNonNull(ipv4Address, "ipv4Address") if null should be a programming error.
  3. If reading from a config file, validate the property is present and non-empty at startup.

Example fix

// before
String ipv6 = IPv6Converter.ipv4ToIpv6(ipv4Str); // ipv4Str may be null or empty

// after
if (ipv4Str == null || ipv4Str.isEmpty()) {
    throw new IllegalArgumentException("IPv4 address must not be null or empty");
}
String ipv6 = IPv6Converter.ipv4ToIpv6(ipv4Str);
Defensive patterns

Strategy: validation

Validate before calling

if (ipv4Address == null || ipv4Address.isEmpty()) {
    throw new IllegalArgumentException("IPv4 address must not be null or empty");
}
String result = IPv6Converter.ipv4ToIpv6(ipv4Address);

Type guard

static boolean isValidIpv4String(String addr) {
    return addr != null && !addr.isEmpty() && addr.matches("^\\d{1,3}(\\.\\d{1,3}){3}$");
}

Try / catch

try {
    String ipv6 = IPv6Converter.ipv4ToIpv6(ipv4Address);
} catch (UnknownHostException e) {
    // null, empty, or unresolvable address; handle gracefully
    logger.warn("Invalid IPv4 address: {}", ipv4Address);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/1a7c668969eb9880. Report an issue: GitHub.