stride3d/stride · error · ArgumentNullException

Cannot add a null component

Error message

Cannot add a null component

What it means

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.

Solutions

  1. Ensure the component is instantiated before adding: entity.Components.Add(new MyComponent())
  2. Skip null entries in the source collection before adding
  3. If intentionally removing, call Remove instead of adding null

Example fix

// before
entity.Components.Add(maybeNullComponent);
// after
if (maybeNullComponent != null)
    entity.Components.Add(maybeNullComponent);
Defensive patterns

Strategy: validation

Validate before calling

if (components.Any(c => c == null)) throw new ArgumentException("Component list contains null entries");
foreach (var c in components.Where(c => c != null)) entity.Components.Add(c);

Type guard

static bool IsAddable(Stride.Engine.EntityComponent c) => c != null;

Try / catch

try { entity.Components.Add(component); }
catch (ArgumentNullException ex) when (ex.Message.Contains("null component")) { /* log and skip */ }

Prevention

When it happens

Trigger: Calling entity.Components.Add(null) or entity.Components.Insert(index, null); adding from a loop over an array/list that contains null entries.

Common situations: 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.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/c0e8840ba71e4874. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Engine/Engine/EntityComponentCollection.cs:208

            var oldItem = ValidateItem(index, item, true);

            if (item != oldItem)
            {
                // Detach entity from previous item only when it's different from the new item.
                oldItem.Entity = null;
            }

            base.SetItem(index, item);

            // Notify the entity about this component being updated
            entity?.OnComponentChanged(index, oldItem, item);
        }

        private EntityComponent ValidateItem(int index, EntityComponent item, bool isReplacing)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item), @"Cannot add a null component");
            }

            var componentType = item.GetType();
            var attributes = EntityComponentAttributes.Get(componentType);

            var onlySingleComponent = !attributes.AllowMultipleComponents;

            EntityComponent previousItem = null;
            for (int i = 0; i < Count; i++)
            {
                var existingItem = this[i];
                if (index == i && isReplacing)
                {
                    previousItem = existingItem;
                }
                else
                {
                    if (ReferenceEquals(existingItem, item))

View on GitHub (pinned to 96fad776d2)