jwtk/jjwt · error · IllegalArgumentException

${i.getClass()} getId() cannot be null or empty.

Error message

${i.getClass()} getId() cannot be null or empty.

What it means

NestedIdentifiableCollection.assertId validates that every element added to the collection implements Identifiable and returns a non-null, non-empty id via getId(). If the id is missing, an IllegalArgumentException naming the element's class is thrown. This guards collection identity integrity.

Source

Thrown at impl/src/main/java/io/jsonwebtoken/impl/lang/NestedIdentifiableCollection.java:53

    private final P PARENT;
    private final Map<String, E> VALUES;

    private static <K, V> Map<K, V> nullSafe(Map<K, V> m) {
        return m == null ? Collections.<K, V>emptyMap() : m;
    }

    public NestedIdentifiableCollection(P parent, Map<String, ? extends E> seed) {
        super();
        this.PARENT = Assert.notNull(parent, "parent cannot be null.");
        this.VALUES = new LinkedHashMap<>(nullSafe(seed));
    }

    protected final String assertId(E i) {
        Assert.notNull(i, "Identifiable instance cannot be null.");
        String id = i.getId();
        if (!Strings.hasText(id)) {
            String msg = i.getClass() + " getId() cannot be null or empty.";
            throw new IllegalArgumentException(msg);
        }
        return id;
    }

    private boolean doAdd(E e) {
        String id = assertId(e);
        this.VALUES.put(id, e);
        return true;
    }

    @Override
    public NestedCollection<E, P> add(E e) {
        if (e != null) {
            doAdd(e);
            changed();
        }
        return this;
    }

View on GitHub (pinned to fb71496164)

Solutions

  1. Set a non-empty id on the object before adding it to the collection.
  2. Ensure getId() never returns null/empty once the object is constructed (assign in constructor or builder).
  3. Validate the id with a hasText check before calling add().

Example fix

// before
myIdentifiable.setId("");
collection.add(myIdentifiable);
// after
myIdentifiable.setId("unique-key-1");
collection.add(myIdentifiable);
Defensive patterns

Strategy: validation

Validate before calling

void safeAdd(Collection<E> c, E item) {
    if (item == null || item.getId() == null || item.getId().isBlank()) {
        throw new IllegalArgumentException("Item must have a non-empty id before adding");
    }
    c.add(item);
}

Try / catch

try {
    collection.add(item);
} catch (IllegalArgumentException e) {
    if (e.getMessage() != null && e.getMessage().endsWith("getId() cannot be null or empty.")) {
        // assign an id and retry or report
    }
    throw e;
}

Prevention

When it happens

Trigger: Adding an Identifiable element whose getId() returns null or "" to a NestedIdentifiableCollection (e.g. via add/doAdd), or passing a null element.

Common situations: Custom Identifiable implementations that lazily assign ids; deserialized objects with ids not yet populated; builder misuse where an id setter was skipped.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of jwtk/jjwt@fb71496164 (2026-09-09). Data as JSON: /api/errors/92a2b2afe8005644. Report an issue: GitHub.