gradle/gradle · error · IllegalArgumentException

Cannot add an element of type %s to a collection of %s

Error message

Cannot add an element of type %s to a collection of %s

What it means

Error "Cannot add an element of type %s to a collection of %s" thrown in gradle/gradle.

Source

Thrown at platforms/core-configuration/model-core/src/main/java/org/gradle/model/internal/manage/schema/extract/ScalarCollectionNodeInitializerExtractionStrategy.java:171

        public MutationSafeCollection(C delegate, ModelViewState state, ModelType<T> elementType) {
            this.delegate = delegate;
            this.state = state;
            this.elementType = elementType;
        }

        public C getDelegate(boolean forMutation) {
            if (forMutation) {
                state.assertCanMutate();
            }
            return delegate;
        }

        protected void validateElementType(Object o) {
            if (o != null) {
                ModelType<?> obType = ModelType.of(o.getClass());
                if (!obType.equals(elementType)) {
                    throw new IllegalArgumentException(String.format("Cannot add an element of type %s to a collection of %s", obType, elementType));
                }
            }
        }

        protected void validateCollection(Collection<? extends T> c) {
            for (T element : c) {
                validateElementType(element);
            }
        }

        @Override
        public boolean add(T t) {
            validateElementType(t);
            return getDelegate(true).add(t);
        }

        @Override
        public boolean addAll(Collection<? extends T> c) {

View on GitHub (pinned to 534f27719b)

Solutions

  1. An element of the wrong type was added to a scalar collection. Add only elements of the collection's declared element type.

When it happens

Trigger: Thrown at platforms/core-configuration/model-core/src/main/java/org/gradle/model/internal/manage/schema/extract/ScalarCollectionNodeInitializerExtractionStrategy.java:171 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of gradle/gradle@534f27719b (2026-08-22). Data as JSON: /api/errors/86e273e335915330. Report an issue: GitHub.