stride3d/stride · error · YamlException

Unexpected null scalar value

Error message

Unexpected null scalar value

What it means

PrimitiveSerializer.ConvertFrom encountered a null scalar for a target type whose TypeCode is not Object/Empty/String/nullable — i.e. a value type like int, bool, or DateTime. There is no sensible null representation, so this YamlException is thrown.

Solutions

  1. Provide a concrete value in the YAML for the primitive field instead of null/empty
  2. Make the target property nullable (int?, bool?) or use a default value strategy
  3. Pre-process the YAML to replace null scalars with defaults before deserialization
  4. Enable AllowErrors if skipping such members is acceptable

Example fix

// before
port: null   // property is int
// after
port: 8080   // or change property to int? Port { get; set; }
Defensive patterns

Strategy: type-guard

Validate before calling

if (scalar == null || scalar.Value == null || scalar.Value is "" or "~" or "null")
{
    if (targetType.IsValueType && Nullable.GetUnderlyingType(targetType) == null)
        throw new YamlException(scalar?.Start, scalar?.End, $"{targetType} cannot be null; provide a value");
}

Type guard

static bool AcceptsNull(Type t) =>
    !t.IsValueType || Nullable.GetUnderlyingType(t) != null || t == typeof(object) || t == typeof(string);

Try / catch

try
{
    primitiveSerializer.ReadYaml(ref objectContext);
}
catch (YamlException ex) when (ex.Message == "Unexpected null scalar value")
{
    // substitute a default value or make the member nullable
}

Prevention

When it happens

Trigger: A YAML node contains 'null'/'~'/empty scalar but the conversion target is a non-nullable primitive (int, double, bool, enum-backed non-object typecodes). ConvertFrom reaches the default branch and throws.

Common situations: YAML file where a numeric field is left empty or set to null; schema change making a formerly-string property a primitive; tools writing '~' for missing values.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Yaml/Serialization/Serializers/PrimitiveSerializer.cs:78

        public override object ConvertFrom(ref ObjectContext context, Scalar scalar)
        {
            var primitiveType = (PrimitiveDescriptor) context.Descriptor;
            var type = primitiveType.Type;
            var text = scalar.Value;

            // Return null if expected type is an object and scalar is null
            if (text == null)
            {
                switch (Type.GetTypeCode(type))
                {
                    case TypeCode.Object:
                    case TypeCode.Empty:
                    case TypeCode.String:
                        return null;
                    default:
                        // TODO check this
                        throw new YamlException(scalar.Start, scalar.End, "Unexpected null scalar value");
                }
            }

            // If type is an enum, try to parse it
            if (type.IsEnum)
            {
                bool enumRemapped;
                var result = primitiveType.ParseEnum(text, out enumRemapped);
                if (enumRemapped)
                {
                    context.SerializerContext.HasRemapOccurred = true;
                }
                return result;
            }

            // Parse default types 
            switch (Type.GetTypeCode(type))
            {

View on GitHub (pinned to 96fad776d2)