stride3d/stride · error · ArgumentNullException

Value cannot be null. (Parameter 'element')

Error message

Value cannot be null. (Parameter 'element')

What it means

Argument-null validation in UICloner.Clone: the element (UIElement) to clone is null. The cloner creates a copy of a library UI element for a UI component; with no source element there is nothing to clone, so it rejects the call with ArgumentNullException named 'element' ( surfaced as 'Value cannot be null. (Parameter element)').

Solutions

  1. Null-check the source element before cloning
  2. When using library instantiation, verify the element name exists in library.UIElements before cloning
  3. Fix the asset/library so the referenced UI element actually exists

Example fix

// before
var copy = UICloner.Clone(rootElement.Child); // Child is null -> throws
// after
if (rootElement.Child != null)
    copy = UICloner.Clone(rootElement.Child);
Defensive patterns

Strategy: validation

Validate before calling

if (element == null)
    return null; // or log which element failed to resolve

Type guard

bool CanClone(UIElement e) => e != null;

Try / catch

try
{
    var copy = UICloner.Clone(element);
}
catch (ArgumentNullException ex) when (ex.ParamName == "element")
{
    // the source element was never loaded/found; fix asset or lookup
}

Prevention

When it happens

Trigger: Calling UICloner.Clone(null) directly; instantiating a UI library element whose lookup returned null and then cloning it; a UILibrary.UIElements dictionary entry that is null.

Common situations: Instantiating prefabs from a library where the named element is missing or the library failed to load; editor scripts cloning a selection that resolved to null; deserialization leaving a UIElement reference null.

Related errors


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

Appendix: source

Thrown at sources/engine/Stride.UI/Engine/Design/UICloner.cs:24

using System.IO;
using Stride.Core.Serialization;
using Stride.UI;

namespace Stride.Engine.Design
{
    public class UICloner
    {
        private static readonly EntityCloner.CloneContext CloneContext = new EntityCloner.CloneContext();
        private static SerializerSelector cloneSerializerSelector;

        // CloneObject TLS used to clone objects, so that we don't create one everytime we clone
        [ThreadStatic] private static HashSet<object> clonedElementsLazy;

        private static HashSet<object> ClonedElements => clonedElementsLazy ?? (clonedElementsLazy = new HashSet<object>());

        public static UIElement Clone(UIElement element)
        {
            if (element == null) throw new ArgumentNullException(nameof(element));
            var clonedObjects = ClonedElements;
            try
            {
                clonedObjects.Add(element);
                var clone = Clone(clonedObjects, element);
                return clone;
            }
            finally
            {
                clonedObjects.Clear();
            }
        }

        private static T Clone<T>(HashSet<object> clonedObjects, T entity) where T : class
        {
            if (cloneSerializerSelector == null)
            {
                cloneSerializerSelector = new SerializerSelector(true, false, "Default", "Clone");

View on GitHub (pinned to 96fad776d2)