{"record":{"id":"9559e4db34a38f22","repo":"stride3d/stride","slug":"cannot-add-an-empty-asset-item-reference","errorCode":null,"errorMessage":"Cannot add an empty asset item reference","messagePattern":"Cannot add an empty asset item reference","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"sources/assets/Stride.Core.Assets/PackageAssetCollection.cs","lineNumber":315,"sourceCode":"    /// item;Location cannot be null when adding an asset reference\n    /// </exception>\n    /// <exception cref=\"ArgumentException\">\n    /// An asset with the same location is already registered [{0}].ToFormat(location.Path);item\n    /// or\n    /// An asset with the same id [{0}] is already registered with the location [{1}].ToFormat(item.Id, location.Path);item\n    /// or\n    /// Asset location [{0}] cannot contain drive information.ToFormat(location);item\n    /// or\n    /// Asset location [{0}] must be relative and not absolute (not start with '/').ToFormat(location);item\n    /// or\n    /// Asset location [{0}] cannot start with relative '..'.ToFormat(location);item\n    /// </exception>\n    public void CheckCanAdd(AssetItem item)\n    {\n        // TODO better handle interaction\n        if (item == null)\n        {\n            throw new ArgumentNullException(nameof(item), \"Cannot add an empty asset item reference\");\n        }\n\n        if (registeredItems.Contains(item))\n        {\n            throw new ArgumentException(\"Asset already exist in this collection\", nameof(item));\n        }\n\n        if (item.Id == AssetId.Empty)\n        {\n            throw new ArgumentException(\"Cannot add an asset with an empty Id\", nameof(item));\n        }\n\n        if (item.Package != null && item.Package != Package)\n        {\n            throw new ArgumentException(\"Cannot add an asset that is already added to another package\", nameof(item));\n        }\n\n        // Note: we ignore name collisions if asset is not referenceable","sourceCodeStart":297,"sourceCodeEnd":333,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/assets/Stride.Core.Assets/PackageAssetCollection.cs#L297-L333","documentation":"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.","triggerScenarios":"Calling Add/CheckCanAdd with a null AssetItem, often the result of a lookup/parse step that returned null and was passed through unchecked.","commonSituations":"Dictionaries or asset-loading code returning null for missing assets and the value being added directly; refactored pipelines losing null checks.","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"],"exampleFix":"// before\nassets.Add(FindAsset(id));\n// after\nvar item = FindAsset(id);\nif (item != null) assets.Add(item);","handlingStrategy":"type-guard","validationCode":"if (item is null) return; // skip or log instead of adding","typeGuard":"bool CanAdd(PackageAssetCollection c, AssetItem i) => i is not null && !c.Contains(i);","tryCatchPattern":"try { assets.Add(item); } catch (ArgumentNullException ex) { log.Warn($\"Skipped null asset item: {ex.ParamName}\"); }","preventionTips":["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"],"tags":["null","argument","assets"],"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"}