dotnet/machinelearning · error · ArgumentNullException
Value cannot be null. (Parameter 'getter') Getter of type '{
Error message
Value cannot be null. (Parameter 'getter')
Getter of type '{typeof(TValue)}' expected, but {getter.GetType()} found What it means
CheckGetter<TValue> verifies each annotation getter is a ValueGetter<TValue> matching the declared annotation column's RawType; if the delegate exists but has the wrong type, it throws ArgumentNullException naming 'getter' with a message showing the expected TValue and the actual delegate type. This is a type-conformance check even though it throws ArgumentNullException.
Source
Thrown at src/Microsoft.ML.DataView/DataViewSchema.cs:215
Debug.Assert(schema.Count == getters.Length);
// Check all getters.
for (int i = 0; i < schema.Count; i++)
{
var getter = getters[i];
if (getter == null)
throw new ArgumentNullException(nameof(getter), $"Delegate at index '{i}' of {nameof(getters)} was null.");
Utils.MarshalActionInvoke(CheckGetter<int>, schema[i].Type.RawType, getter);
}
Schema = schema;
_getters = getters;
}
private void CheckGetter<TValue>(Delegate getter)
{
var typedGetter = getter as ValueGetter<TValue>;
if (typedGetter == null)
throw new ArgumentNullException(nameof(getter), $"Getter of type '{typeof(TValue)}' expected, but {getter.GetType()} found");
}
/// <summary>
/// Get a getter delegate for one value of the annotations row.
/// </summary>
public ValueGetter<TValue> GetGetter<TValue>(DataViewSchema.Column column)
{
if (column.Index >= _getters.Length)
throw new ArgumentException(nameof(column));
var typedGetter = _getters[column.Index] as ValueGetter<TValue>;
if (typedGetter == null)
{
Debug.Assert(_getters[column.Index] != null);
throw new InvalidOperationException($"Invalid call to '{nameof(GetGetter)}'");
}
return typedGetter;
}
View on GitHub (pinned to 7b76e69cf9)
Solutions
- Align each getter to ValueGetter<TValue> where TValue equals the annotation column's Type.RawType.
- Cast/construct the delegate explicitly as (ValueGetter<TValue>)((ref TValue value) => ...) so the compiler enforces the type.
- Fix the annotation schema declaration to match the getter type actually provided.
- Add a unit test constructing the Annotations to catch type drift early.
Example fix
// before
Delegate g = (ValueGetter<float>)(ref float v) => v = 1f; // annotation slot is RawType double
new Annotations(schema, new Delegate[] { g });
// after
Delegate g = (ValueGetter<double>)(ref double v) => v = 1.0; // matches RawType
new Annotations(schema, new Delegate[] { g }); Defensive patterns
Strategy: type-guard
Validate before calling
bool ok = getter is ValueGetter<double> && schema[0].Type.RawType == typeof(double);
Type guard
bool IsTypedGetter<TValue>(Delegate g) => g is ValueGetter<TValue>;
Prevention
- Declare getters as explicit ValueGetter<TValue> so the compiler catches mismatches
- Keep annotation schema types and getter types in sync; test Annotations construction
When it happens
Trigger: Passing a Delegate to Annotations whose runtime type is not ValueGetter<TValue> for the corresponding schema slot's RawType — e.g. a ValueGetter<float> supplied where the annotation type is double, or a plain lambda typed as Action/Func instead of ValueGetter.
Common situations: Mismatched annotation type declarations vs. getter implementations; refactoring that changed TValue of the getter but not the annotation schema; boxing conversions (int getter for uint annotation).
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Value cannot be null. (Parameter 'getter') Delegate at index
- String.Format(Strings.MismatchedColumnValueType, this.DataTy
- Strings.BadColumnCast (formatted with column.DataType, typeo
- Strings.BadColumnCast (formatted with column.DataType, typeo
- Strings.BadColumnCast (formatted with column.DataType, typeo
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/aa6bb02406bc9aeb.
Report an issue: GitHub.