stride3d/stride · error · ArgumentNullException

factory

Error message

factory

What it means

SerializerFactorySelector.TryAddFactory throws ArgumentNullException("factory") when a null IYamlSerializableFactory is registered. Factories must be non-null so the selector can later query them to produce serializers.

Solutions

  1. Pass a valid IYamlSerializableFactory instance.
  2. Fix the construction/resolution path that produced null.
  3. Guard registration code with a null check and log/skip.
  4. Catch ArgumentNullException around registration during plugin loading.
  5. Register factories explicitly with `new` rather than via possibly-null variables.

Example fix

// before
selector.TryAddFactory(customFactory); // null
// after
if (customFactory != null) selector.TryAddFactory(customFactory);
Defensive patterns

Strategy: validation

Validate before calling

if (factory == null) throw new ArgumentException("Serializer factory must not be null", nameof(factory));

Type guard

static bool ValidFactory(IYamlSerializableFactory f) => f != null;

Try / catch

try { selector.TryAddFactory(factory); }
catch (ArgumentNullException ex) { log.Warn("Skipping null serializer factory registration", ex); }

Prevention

When it happens

Trigger: Calling TryAddFactory(null), usually because a factory instance field was never initialized or a DI container resolved to null.

Common situations: Optional custom serializer factories that failed to construct; configuration-driven registration where the type name resolved to null; ordering bugs where registration happens before construction.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Yaml/Serialization/SerializerFactorySelector.cs:23

using System.Threading;
using Stride.Core.Reflection;

namespace Stride.Core.Yaml.Serialization
{
    /// <summary>
    /// Base class that implements <see cref="ISerializerFactorySelector"/>.
    /// </summary>
    public abstract class SerializerFactorySelector : ISerializerFactorySelector
    {
        private readonly Dictionary<Type, IYamlSerializable> serializers = new Dictionary<Type, IYamlSerializable>();
        private readonly List<IYamlSerializableFactory> factories = new List<IYamlSerializableFactory>();
        private readonly ReaderWriterLockSlim serializerLock = new ReaderWriterLockSlim();
        private bool isSealed;

        /// <inheritdoc/>
        public void TryAddFactory(IYamlSerializableFactory factory)
        {
            if (factory == null) throw new ArgumentNullException(nameof(factory));
            if (isSealed) throw new InvalidOperationException("Cannot add a factory to a serializer factory selector once it is sealed.");
            if (CanAddSerializerFactory(factory))
            {
                factories.Add(factory);
            }
        }

        /// <inheritdoc/>
        public void Seal()
        {
            isSealed = true;
        }

        /// <inheritdoc/>
        public IYamlSerializable GetSerializer(SerializerContext context, ITypeDescriptor typeDescriptor)
        {
            if (!isSealed) throw new InvalidOperationException("A serializer factory selector must be sealed before being used.");
            IYamlSerializable serializer;

View on GitHub (pinned to 96fad776d2)