{"record":{"id":"86e273e335915330","repo":"gradle/gradle","slug":"cannot-add-an-element-of-type-s-to-a-collection-o","errorCode":null,"errorMessage":"Cannot add an element of type %s to a collection of %s","messagePattern":"Cannot add an element of type (.+?) to a collection of (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"platforms/core-configuration/model-core/src/main/java/org/gradle/model/internal/manage/schema/extract/ScalarCollectionNodeInitializerExtractionStrategy.java","lineNumber":171,"sourceCode":"\n        public MutationSafeCollection(C delegate, ModelViewState state, ModelType<T> elementType) {\n            this.delegate = delegate;\n            this.state = state;\n            this.elementType = elementType;\n        }\n\n        public C getDelegate(boolean forMutation) {\n            if (forMutation) {\n                state.assertCanMutate();\n            }\n            return delegate;\n        }\n\n        protected void validateElementType(Object o) {\n            if (o != null) {\n                ModelType<?> obType = ModelType.of(o.getClass());\n                if (!obType.equals(elementType)) {\n                    throw new IllegalArgumentException(String.format(\"Cannot add an element of type %s to a collection of %s\", obType, elementType));\n                }\n            }\n        }\n\n        protected void validateCollection(Collection<? extends T> c) {\n            for (T element : c) {\n                validateElementType(element);\n            }\n        }\n\n        @Override\n        public boolean add(T t) {\n            validateElementType(t);\n            return getDelegate(true).add(t);\n        }\n\n        @Override\n        public boolean addAll(Collection<? extends T> c) {","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/gradle/gradle/blob/534f27719b66953f95cc907aae7f2c1b12f5482d/platforms/core-configuration/model-core/src/main/java/org/gradle/model/internal/manage/schema/extract/ScalarCollectionNodeInitializerExtractionStrategy.java#L153-L189","documentation":"Error \"Cannot add an element of type %s to a collection of %s\" thrown in gradle/gradle.","triggerScenarios":"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.","commonSituations":"See trigger scenarios.","solutions":["An element of the wrong type was added to a scalar collection. Add only elements of the collection's declared element type."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"534f27719b66953f95cc907aae7f2c1b12f5482d","analyzedAt":"2026-08-22T08:09:12.375Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}