{"record":{"id":"c0e8840ba71e4874","repo":"stride3d/stride","slug":"cannot-add-a-null-component","errorCode":null,"errorMessage":"Cannot add a null component","messagePattern":"Cannot add a null component","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Engine/Engine/EntityComponentCollection.cs","lineNumber":208,"sourceCode":"            var oldItem = ValidateItem(index, item, true);\n\n            if (item != oldItem)\n            {\n                // Detach entity from previous item only when it's different from the new item.\n                oldItem.Entity = null;\n            }\n\n            base.SetItem(index, item);\n\n            // Notify the entity about this component being updated\n            entity?.OnComponentChanged(index, oldItem, item);\n        }\n\n        private EntityComponent ValidateItem(int index, EntityComponent item, bool isReplacing)\n        {\n            if (item == null)\n            {\n                throw new ArgumentNullException(nameof(item), @\"Cannot add a null component\");\n            }\n\n            var componentType = item.GetType();\n            var attributes = EntityComponentAttributes.Get(componentType);\n\n            var onlySingleComponent = !attributes.AllowMultipleComponents;\n\n            EntityComponent previousItem = null;\n            for (int i = 0; i < Count; i++)\n            {\n                var existingItem = this[i];\n                if (index == i && isReplacing)\n                {\n                    previousItem = existingItem;\n                }\n                else\n                {\n                    if (ReferenceEquals(existingItem, item))","sourceCodeStart":190,"sourceCodeEnd":226,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Engine/Engine/EntityComponentCollection.cs#L190-L226","documentation":"Entity.Components (an EntityComponentCollection) validates every inserted item and throws ArgumentNullException with this message when a null component is added. A collection of components cannot contain nulls, so the library rejects them at insertion rather than failing later during processing.","triggerScenarios":"Calling entity.Components.Add(null) or entity.Components.Insert(index, null); adding from a loop over an array/list that contains null entries.","commonSituations":"Data-driven scene setup where component lists come from config or deserialization and some entries are null; refactoring that removed component instantiation but left the Add call.","solutions":["Ensure the component is instantiated before adding: entity.Components.Add(new MyComponent())","Skip null entries in the source collection before adding","If intentionally removing, call Remove instead of adding null"],"exampleFix":"// before\nentity.Components.Add(maybeNullComponent);\n// after\nif (maybeNullComponent != null)\n    entity.Components.Add(maybeNullComponent);","handlingStrategy":"validation","validationCode":"if (components.Any(c => c == null)) throw new ArgumentException(\"Component list contains null entries\");\nforeach (var c in components.Where(c => c != null)) entity.Components.Add(c);","typeGuard":"static bool IsAddable(Stride.Engine.EntityComponent c) => c != null;","tryCatchPattern":"try { entity.Components.Add(component); }\ncatch (ArgumentNullException ex) when (ex.Message.Contains(\"null component\")) { /* log and skip */ }","preventionTips":["Initialize component fields inline or in constructors","Validate deserialized component lists before applying them","Use Remove for removals, never Add(null)"],"tags":["csharp","stride","null-reference","argument-null"],"backgroundTag":"null-argument","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}