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
- Null-check the AssetItem before calling Add
- Fix the upstream lookup that returns null instead of a valid item
- 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
- Check lookup results for null before adding
- Make asset-loading helpers return empty instead of null
- Enable nullable reference types to catch this at compile time
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
- A part Id cannot be empty.
- Value cannot be null. (Parameter 'part')
- filePath must be relative
- Cannot find the specified AssetItem instance in the session
- Could not find dependency
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 referenceableView on GitHub (pinned to 96fad776d2)