dotnet/machinelearning · error · ArgumentNullException

getter

Error message

getter

What it means

Builder.Add<TValue> requires a non-null ValueGetter<TValue>; the delegate is what cursors will call to read the value, so a null getter would crash later at row-iteration time. The builder throws ArgumentNullException 'getter' eagerly instead.

Source

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

                }

                /// <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)
                {
                    if (string.IsNullOrEmpty(name))

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Always supply a real delegate, e.g. (ref float v) => v = 0 for stubs
  2. Bind the getter to the source row's GetGetter: value => src.GetGetter<TValue>(col)(ref value) pattern
  3. If the column should not exist, do not Add it at all rather than adding a null getter

Example fix

// before
builder.Add("Score", type, null);
// after
var srcGetter = input.Row.GetGetter<float>(input.Schema["Score"]);
builder.Add("Score", type, (ref float v) => srcGetter(ref v));
Defensive patterns

Strategy: validation

Validate before calling

if (getter == null)
    getter = (ValueGetter<TValue>)((ref TValue v) => v = default);
builder.Add(name, type, getter);

Try / catch

try { builder.Add(name, type, getter); }
catch (ArgumentNullException e) when (e.ParamName == "getter")
{
    builder.Add(name, type, (ref TValue v) => v = default);
}

Prevention

When it happens

Trigger: Passing null for a placeholder column you intended to fill later; passing a method group whose signature doesn't match ValueGetter<TValue> so the compiler resolved null via a conditional expression; wiring a getter from a disposed/absent source row.

Common situations: Custom transforms that copy some columns but stub others with null; generated schema code where getter assignment is conditional.

Related errors


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