stride3d/stride · error · ArgumentNullException

emitter

Error message

emitter

What it means

Serializer.Serialize validates its IEmitter argument and throws ArgumentNullException("emitter") when null. The emitter is the output sink for serialization, so there is nothing meaningful to do without it.

Solutions

  1. Pass a constructed emitter (e.g. new Emitter(writer)) to Serialize
  2. Create the emitter from a validated TextWriter/Stream before calling Serialize
  3. Guard the emitter creation path so it never returns null (throw earlier with context)
  4. Use the string Serialize overloads that build the emitter internally

Example fix

// before
serializer.Serialize(emitter, graph, type); // emitter == null
// after
using var writer = new StreamWriter(path);
serializer.Serialize(new Emitter(writer), graph, type);
Defensive patterns

Strategy: type-guard

Validate before calling

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

Type guard

static bool CanSerialize(IEmitter e) => e != null;

Try / catch

try { serializer.Serialize(emitter, graph, type); }
catch (ArgumentNullException ex) when (ex.ParamName == "emitter") { throw new InvalidOperationException("Emitter not initialized", ex); }

Prevention

When it happens

Trigger: Calling Serialize(null, graph, type) — e.g. an emitter variable that failed to initialize from a file/stream that could not be opened, or a method returning null emitter.

Common situations: Conditional emitter creation where output setup failed silently; refactors that removed emitter instantiation; DI containers returning null for IEmitter.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Yaml/Serialization/Serializer.cs:201

        /// Serializes the specified object.
        /// </summary>
        /// <param name="emitter">The <see cref="IEmitter" /> where to serialize the object.</param>
        /// <param name="graph">The object to serialize.</param>
        public void Serialize(IEmitter emitter, object graph)
        {
            Serialize(emitter, graph, graph == null ? typeof(object) : null);
        }

        /// <summary>
        /// Serializes the specified object.
        /// </summary>
        /// <param name="emitter">The <see cref="IEmitter" /> where to serialize the object.</param>
        /// <param name="graph">The object to serialize.</param>
        /// <param name="type">The static type of the object to serialize.</param>
        /// <param name="contextSettings">The context settings.</param>
        public void Serialize(IEmitter emitter, object graph, Type type, SerializerContextSettings contextSettings = null)
        {
            if (emitter == null) throw new ArgumentNullException(nameof(emitter));

            if (graph == null && type == null) throw new ArgumentNullException(nameof(type));

            // Configure the emitter
            // TODO the current emitter is not enough configurable to format its output
            // This should be improved
            var defaultEmitter = emitter as Emitter;
            if (defaultEmitter != null)
            {
                defaultEmitter.ForceIndentLess = Settings.IndentLess;
            }

            var context = new SerializerContext(this, contextSettings) { Emitter = emitter, Writer = CreateEmitter(emitter) };

            // Serialize the document
            context.Writer.StreamStart();
            context.Writer.DocumentStart();
            var objectContext = new ObjectContext(context, graph, context.FindTypeDescriptor(type)) { Style = DataStyle.Any };

View on GitHub (pinned to 96fad776d2)