dotnet/machinelearning · error · ArgumentNullException
type
Error message
type
What it means
Null-argument guard on the type parameter of DataViewSchema.Builder.Add (the strongly-typed annotation overload used by AddPrimitiveValue, GetSummaryIRowOrNull and MakeStatisticsMetadata). An annotation column cannot exist without a DataViewType, so passing null for type throws an ArgumentNullException naming 'type'; name and getter have their own separate checks.
Source
Thrown at src/Microsoft.ML.DataView/DataViewSchema.cs:300
_items.Add((column.Name, column.Type, annotations.GetGetterInternal(column.Index), column.Annotations));
}
}
/// <summary>
/// Add one annotation column, strongly-typed version.
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="name">The annotation name.</param>
/// <param name="type">The annotation type.</param>
/// <param name="getter">The getter delegate.</param>
/// <param name="annotations">Annotations of the input column. Note that annotations on an annotation column is somewhat rare
/// except for certain types (for example, slot names for a vector, key values for something of key type).</param>
public void Add<TValue>(string name, DataViewType type, ValueGetter<TValue> getter, Annotations annotations = null)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException(nameof(name));
if (type == null)
throw new ArgumentNullException(nameof(type));
if (getter == null)
throw new ArgumentNullException(nameof(getter));
if (type.RawType != typeof(TValue))
throw new ArgumentException($"{nameof(type)}.{nameof(type.RawType)} must be of type '{typeof(TValue).FullName}'.", nameof(type));
_items.Add((name, type, getter, annotations));
}
/// <summary>
/// Add one annotation column, weakly-typed version.
/// </summary>
/// <param name="name">The annotation name.</param>
/// <param name="type">The annotation type.</param>
/// <param name="getter">The getter delegate that provides the value. Note that the type of the getter is still checked
/// inside this method.</param>
/// <param name="annotations">Annotations of the input column. Note that annotations on an annotation column is somewhat rare
/// except for certain types (for example, slot names for a vector, key values for something of key type).</param>
public void Add(string name, DataViewType type, Delegate getter, Annotations annotations = null)View on GitHub (pinned to 7b76e69cf9)
Solutions
- Resolve the type from an existing column: var type = schema[name].Type and verify the column exists first
- Use a concrete type such as NumberDataViewType.Single or a keyed/vector type factory instead of null
- Fail fast with a clear message if the source type cannot be determined
Example fix
// before
builder.Add("Score", null, getter);
// after
var type = input.Schema["Score"].Type;
builder.Add("Score", type, getter); Defensive patterns
Strategy: validation
Validate before calling
if (type == null)
type = source.Schema["Score"]?.Type ?? NumberDataViewType.Single;
builder.Add(name, type, getter); Try / catch
try { builder.Add(name, type, getter); }
catch (ArgumentNullException e) when (e.ParamName == "type")
{
builder.Add(name, NumberDataViewType.Single, getter);
} Prevention
- Resolve DataViewType from an existing schema column rather than constructing null
- Null-check any type-lookup result before use
- Use well-known type singletons (NumberDataViewType.*, new VectorDataViewType(...)) instead of null placeholders
When it happens
Trigger: Passing null because the type was obtained from a lookup that failed (e.g. GetColumnOrNull returned null and .Type was dereferenced from a null); constructing types conditionally and leaving a null path.
Common situations: Custom transform output schema built from input columns that may be absent; annotation types fetched from a missing source column.
Related errors
- name
- selector
- getter
- The provided model file {filePath} doesn't exist.
- Start must be called on a ModelLoader before it can be used.
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/42e4a010e183315d.
Report an issue: GitHub.