stride3d/stride · error · ArgumentNullException

Value cannot be null. (Parameter 'library')

Error message

Value cannot be null. (Parameter 'library')

What it means

Argument-null validation in UILibraryExtensions.InstantiateElement<TElement>: the library (UILibrary) extension-method receiver is null. Instantiating an element requires looking up the element by name inside the library; a null library has no elements to search, so the call fails with ArgumentNullException named 'library'.

Solutions

  1. Assign the UILibrary asset reference on the component/script before instantiating
  2. Wait for/load the library asset (Content.Load) before calling InstantiateElement
  3. Null-check the library (or the resolved property) before invoking the extension

Example fix

// before
public UILibrary Library; // left null in editor
var btn = Library.InstantiateElement<Button>("OkButton"); // throws
// after
var btn = Library?.InstantiateElement<Button>("OkButton");
// or check in code: if (Library == null) LoadLibrary();
Defensive patterns

Strategy: validation

Validate before calling

if (library == null || !library.UIElements.ContainsKey(name))
    return null; // element cannot be instantiated

Type guard

bool CanInstantiate(UILibrary lib) => lib?.UIElements != null;

Try / catch

try
{
    var btn = library.InstantiateElement<Button>("OkButton");
}
catch (ArgumentNullException ex) when (ex.ParamName == "library")
{
    // library reference not assigned; load or wire the asset first
}

Prevention

When it happens

Trigger: Calling myLibrary.InstantiateElement<Button>("name") where the library field/property is null (asset not loaded, service not injected, reference not wired in the editor).

Common situations: Forgetting to assign a UILibrary asset reference in a Stride scene/component; loading order issues where the UI is instantiated before the library asset finished loading; script properties left at their defaults.

Related errors


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

Appendix: source

Thrown at sources/engine/Stride.UI/Engine/UILibraryExtensions.cs:22

using System;
using Stride.Engine.Design;
using Stride.UI;

namespace Stride.Engine
{
    public static class UILibraryExtensions
    {
        /// <summary>
        /// Instantiates a copy of the element of the library identified by <paramref name="name"/>.
        /// </summary>
        /// <typeparam name="TElement">The type of the element.</typeparam>
        /// <param name="library">The library.</param>
        /// <param name="name">The name of the element in the library.</param>
        /// <returns></returns>
        public static TElement InstantiateElement<TElement>(this UILibrary library, string name)
            where TElement : UIElement
        {
            if (library == null) throw new ArgumentNullException(nameof(library));

            UIElement source;
            if (library.UIElements.TryGetValue(name, out source))
            {
                return UICloner.Clone(source) as TElement;
            }
            return null;
        }
    }
}

View on GitHub (pinned to 96fad776d2)