elastic/elasticsearch · error · IllegalArgumentException

host cannot be null

Error message

host cannot be null

What it means

The Node constructor that carries metadata requires a non-null HttpHost because every other Node operation (routing, auth cache, connection) keys off host. All other parameters are nullable and left to NodeSelector policy, but host is a structural invariant — a Node without a host cannot exist.

Source

Thrown at client/rest/src/main/java/org/elasticsearch/client/Node.java:70

    private final String version;
    /**
     * Roles that the Elasticsearch process on the host has or {@code null}
     * if we don't know what roles the node has.
     */
    private final Roles roles;
    /**
     * Attributes declared on the node.
     */
    private final Map<String, List<String>> attributes;

    /**
     * Create a {@linkplain Node} with metadata. All parameters except
     * {@code host} are nullable and implementations of {@link NodeSelector}
     * need to decide what to do in their absence.
     */
    public Node(HttpHost host, Set<HttpHost> boundHosts, String name, String version, Roles roles, Map<String, List<String>> attributes) {
        if (host == null) {
            throw new IllegalArgumentException("host cannot be null");
        }
        this.host = host;
        this.boundHosts = boundHosts;
        this.name = name;
        this.version = version;
        this.roles = roles;
        this.attributes = attributes;
    }

    /**
     * Create a {@linkplain Node} without any metadata.
     */
    public Node(HttpHost host) {
        this(host, null, null, null, null, null);
    }

    /**
     * Contact information for the host.

View on GitHub (pinned to db6a809a66)

Solutions

  1. Ensure the HttpHost is resolved and non-null before constructing the Node — validate the source DTO/config.
  2. If the upstream data legitimately lacks a host, skip that node or fix the data producer to include it.
  3. Fail fast at the parsing boundary with a clearer error rather than letting Node throw.

Example fix

// before
Node n = new Node(parsed.host, parsed.boundHosts, parsed.name, ...); // parsed.host == null
// after
Objects.requireNonNull(parsed.host, "nodes-info entry missing host for node " + parsed.name);
Node n = new Node(parsed.host, parsed.boundHosts, parsed.name, ...);
Defensive patterns

Strategy: validation

Validate before calling

Objects.requireNonNull(host, "HttpHost must be resolved before building Node");
Node n = new Node(host, boundHosts, name, version, roles, attributes);

Type guard

static HttpHost requireHost(HttpHost h) {
    if (h == null) throw new IllegalArgumentException("host is required");
    return h;
}

Prevention

When it happens

Trigger: Calling new Node(null, boundHosts, name, version, roles, attributes) — typically because a cluster-info or nodes-info response lacked the host field, or caller code built a Node from a parsed DTO where host was missing.

Common situations: Parsing a malformed nodes-info JSON where the host entry is absent; programmatic Node construction from external config with an unset host; refactoring that drops the host assignment.

Related errors


AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12). Data as JSON: /api/errors/6555a9d719f1f1f8. Report an issue: GitHub.