stride3d/stride · error · ArgumentNullException

type

Error message

type

What it means

Serialize throws ArgumentNullException("type") when both graph and type are null: without a graph instance the static type is the only way to pick a type descriptor, so a null type makes serialization impossible. If graph is non-null its runtime type is inferred and type may be null.

Solutions

  1. Pass the static type explicitly when the graph may be null: Serialize(emitter, null, typeof(MyType))
  2. Ensure graph is non-null before serializing
  3. In generic helpers, fall back to typeof(T) when the instance is null
  4. Throw a clearer domain-specific error before calling Serialize when both inputs are absent

Example fix

// before
serializer.Serialize(emitter, null, null);
// after
serializer.Serialize(emitter, null, typeof(MyPayload));
Defensive patterns

Strategy: type-guard

Validate before calling

if (graph is null && type is null) throw new ArgumentException("Provide the static type when graph is null");

Type guard

static bool SerializeArgsValid(object graph, Type type) => graph != null || type != null;

Try / catch

try { serializer.Serialize(emitter, graph, type); }
catch (ArgumentNullException ex) when (ex.ParamName == "type") { throw new InvalidOperationException("Null graph requires an explicit type", ex); }

Prevention

When it happens

Trigger: Calling Serialize(emitter, null, null) — serializing a null graph without supplying the declared static type.

Common situations: Serializing possibly-null payloads (empty optional fields) without passing typeof(T); generic wrappers that forward null graph and null type.

Related errors


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

Appendix: source

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

        /// <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 };
            context.Serializer.ObjectSerializer.WriteYaml(ref objectContext);
            context.Writer.DocumentEnd();

View on GitHub (pinned to 96fad776d2)