dotnet/machinelearning · error · ArgumentNullException

Value cannot be null. (Parameter 'type')

Error message

Value cannot be null. (Parameter 'type')

What it means

DataViewSchema.Column requires a non-null DataViewType; passing null for 'type' throws ArgumentNullException naming 'type'. The type object defines the column's data type and raw representation, so it is mandatory.

Source

Thrown at src/Microsoft.ML.DataView/DataViewSchema.cs:115

            /// </summary>
            public DataViewType Type { get; }

            /// <summary>
            /// The annotations of the column.
            /// </summary>
            public Annotations Annotations { get; }

            internal Column(string name, int index, bool isHidden, DataViewType type, Annotations annotations)
            {
                if (string.IsNullOrEmpty(name))
                    throw new ArgumentNullException(nameof(name));
                if (index < 0)
                    throw new ArgumentOutOfRangeException(nameof(index));

                Name = name;
                Index = index;
                IsHidden = isHidden;
                Type = type ?? throw new ArgumentNullException(nameof(type));
                Annotations = annotations ?? Annotations.Empty;
            }

            public override string ToString()
            {
                var annotationsString = (Annotations == null || Annotations.Schema.Count == 0) ?
                    null : $" {{{string.Join(", ", Annotations.Schema.Select(x => x.Name))}}}";
                return $"{Name}: {Type}{annotationsString}";
            }
        }

        /// <summary>
        /// This class represents the schema of one column of a data view, without an attachment to a particular <see cref="DataViewSchema"/>.
        /// </summary>
        public struct DetachedColumn
        {
            /// <summary>
            /// The name of the column.

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Ensure a concrete DataViewType (e.g. NumberType.R4, TextType, BooleanDataViewType) is passed; fix the null-producing factory/mapping.
  2. Add an explicit fallback default type for unmapped source types instead of returning null.
  3. Fail fast upstream with a clear message when type inference cannot determine a type.
  4. Prefer builder APIs that compute the type from data rather than passing a raw type argument.

Example fix

// before
DataViewType t = typeMap[sourceKind]; // may return null
var column = new Column(name, index, false, t, null);
// after
DataViewType t = typeMap.TryGetValue(sourceKind, out var mapped) ? mapped : NumberType.R4;
var column = new Column(name, index, false, t, null);
Defensive patterns

Strategy: validation

Validate before calling

if (type == null) throw new ArgumentException("Column type must be resolved before schema construction");

Type guard

bool HasType(DataViewType t) => t != null;

Prevention

When it happens

Trigger: Constructing a Column (or Builder.Add overload) with type == null — e.g. type inference failed upstream, a variable was never assigned, or a factory method returned null for an unsupported data kind.

Common situations: Type-mapping tables missing an entry for a source column type (returning null); loading metadata from files where a type declaration is absent; calling constructors directly instead of builder APIs that infer types.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/a3ab05663c4026ad. Report an issue: GitHub.