stride3d/stride · error · ArgumentNullException

factory

Error message

factory

What it means

LambdaObjectFactory's second constructor validates its Func<Type,object> delegate argument and throws ArgumentNullException named "factory" when it is null. This guard exists because the factory cannot fall back to a null delegate at Create time.

Solutions

  1. Pass a non-null delegate when constructing LambdaObjectFactory
  2. Check the factory delegate for null before constructing and fail fast with a clearer message
  3. Use the nameof-safe overload pattern at your call site to catch the bug early in tests
  4. If the delegate may legitimately be absent, skip registering this factory instead of passing null

Example fix

// before
new LambdaObjectFactory(null, nextFactory)
// after
Func<Type, object> factory = t => Activator.CreateInstance(t);
new LambdaObjectFactory(factory, nextFactory)
Defensive patterns

Strategy: type-guard

Validate before calling

if (factory is null) throw new ArgumentNullException(nameof(factory));

Type guard

static bool IsValidFactory(Func<Type, object> f) => f != null;

Try / catch

try { var f = new LambdaObjectFactory(factory, nextFactory); }
catch (ArgumentNullException ex) { throw new InvalidOperationException("Factory delegate must be provided", ex); }

Prevention

When it happens

Trigger: new LambdaObjectFactory(null, nextFactory) — passing a null factory delegate, typically when the delegate is produced by an expression that returned null or an uninitialized field.

Common situations: Conditional factory setup where a lambda variable is assigned only in some branches; reflection-driven configuration that failed to resolve the factory delegate.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Yaml/Serialization/LambdaObjectFactory.cs:75

        /// <summary>
        /// Initializes a new instance of the <see cref="LambdaObjectFactory"/> class.
        /// </summary>
        /// <param name="factory">The factory.</param>
        public LambdaObjectFactory(Func<Type, object> factory) : this(factory, null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LambdaObjectFactory" /> class.
        /// </summary>
        /// <param name="factory">The factory.</param>
        /// <param name="nextFactory">The next factory.</param>
        /// <exception cref="System.ArgumentNullException">factory</exception>
        public LambdaObjectFactory(Func<Type, object> factory, IObjectFactory nextFactory) : base(nextFactory)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }

            this.factory = factory;
        }

        public override object Create(Type type)
        {
            return factory(type) ?? base.Create(type);
        }
    }
}

View on GitHub (pinned to 96fad776d2)