stride3d/stride · error · ArgumentNullException

Cannot add an empty asset item reference

Error message

Cannot add an empty asset item reference

What it means

PackageAssetCollection.CheckCanAdd validates an AssetItem before it is registered. A null item cannot be added, so it throws ArgumentNullException with the message "Cannot add an empty asset item reference". It is invoked by Add and other mutation entry points.

Solutions

  1. Null-check the AssetItem before calling Add
  2. Fix the upstream lookup that returns null instead of a valid item
  3. Use CheckCanAdd explicitly for pre-validation and handle the null case

Example fix

// before
assets.Add(FindAsset(id));
// after
var item = FindAsset(id);
if (item != null) assets.Add(item);
Defensive patterns

Strategy: type-guard

Validate before calling

if (item is null) return; // skip or log instead of adding

Type guard

bool CanAdd(PackageAssetCollection c, AssetItem i) => i is not null && !c.Contains(i);

Try / catch

try { assets.Add(item); } catch (ArgumentNullException ex) { log.Warn($"Skipped null asset item: {ex.ParamName}"); }

Prevention

When it happens

Trigger: Calling Add/CheckCanAdd with a null AssetItem, often the result of a lookup/parse step that returned null and was passed through unchecked.

Common situations: Dictionaries or asset-loading code returning null for missing assets and the value being added directly; refactored pipelines losing null checks.

Related errors


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

Appendix: source

Thrown at sources/assets/Stride.Core.Assets/PackageAssetCollection.cs:315

    /// item;Location cannot be null when adding an asset reference
    /// </exception>
    /// <exception cref="ArgumentException">
    /// An asset with the same location is already registered [{0}].ToFormat(location.Path);item
    /// or
    /// An asset with the same id [{0}] is already registered with the location [{1}].ToFormat(item.Id, location.Path);item
    /// or
    /// Asset location [{0}] cannot contain drive information.ToFormat(location);item
    /// or
    /// Asset location [{0}] must be relative and not absolute (not start with '/').ToFormat(location);item
    /// or
    /// Asset location [{0}] cannot start with relative '..'.ToFormat(location);item
    /// </exception>
    public void CheckCanAdd(AssetItem item)
    {
        // TODO better handle interaction
        if (item == null)
        {
            throw new ArgumentNullException(nameof(item), "Cannot add an empty asset item reference");
        }

        if (registeredItems.Contains(item))
        {
            throw new ArgumentException("Asset already exist in this collection", nameof(item));
        }

        if (item.Id == AssetId.Empty)
        {
            throw new ArgumentException("Cannot add an asset with an empty Id", nameof(item));
        }

        if (item.Package != null && item.Package != Package)
        {
            throw new ArgumentException("Cannot add an asset that is already added to another package", nameof(item));
        }

        // Note: we ignore name collisions if asset is not referenceable

View on GitHub (pinned to 96fad776d2)