DapperLib/Dapper · error · ArgumentNullException
key
Error message
key
What it means
DapperRow.SetValue throws ArgumentNullException(nameof(key)) when the key is null. DapperRow is a dictionary-like row keyed by column name (resolved via the row's DapperTable), and a null key cannot be indexed. The same method also throws ArgumentException ("An item with the same key has already been added") on duplicate add, but the null-key guard fires first for null inputs.
Source
Thrown at Dapper/SqlMapper.DapperRow.cs:173
if (index < 0 || index >= values.Length || values[index] is DeadValue) return false;
values[index] = DeadValue.Default;
return true;
}
object? IDictionary<string, object?>.this[string key]
{
get { TryGetValue(key, out object? val); return val; }
set { SetValue(key, value, false); }
}
public object? SetValue(string key, object? value)
{
return SetValue(key, value, false);
}
private object? SetValue(string key, object? value, bool isAdd)
{
if (key is null) throw new ArgumentNullException(nameof(key));
int index = table.IndexOfName(key);
if (index < 0)
{
index = table.AddField(key);
}
else if (isAdd && index < values.Length && !(values[index] is DeadValue))
{
// then semantically, this value already exists
throw new ArgumentException("An item with the same key has already been added", nameof(key));
}
return SetValue(index, value);
}
internal object? SetValue(int index, object? value)
{
int oldLength = values.Length;
if (oldLength <= index)
{
// we'll assume they're doing lots of things, and
View on GitHub (pinned to 72a54c475f)
Solutions
- Null-check the key before calling SetValue / before adding to the parameter dictionary.
- Filter out null keys when building dictionaries from dynamic/untrusted sources.
Example fix
// before row.SetValue(colName, val); // colName may be null // after if (colName is not null) row.SetValue(colName, val);
Defensive patterns
Strategy: validation
Validate before calling
if (key is null) throw new ArgumentNullException(nameof(key)); row.SetValue(key, value); // or when building a dictionary: filter null keys first
Type guard
static bool IsValidKey(string? key) => key is not null;
Try / catch
try { row.SetValue(key, value); }
catch (ArgumentNullException ex) when (ex.ParamName == "key")
{ /* skip or default the null key */ } Prevention
- Filter null keys out of dynamic dictionaries before passing them to Dapper.
- Null-check column names derived from lookups/config before SetValue.
When it happens
Trigger: Calling row.SetValue(null, value) directly; a dynamic insert/update path that produced a null column name; passing a dictionary with a null key into a Dapper execute that materializes a DapperRow parameter.
Common situations: Building parameter dictionaries dynamically where a key ended up null (e.g. a column name lookup returned null); deserializing JSON into a dictionary that allowed a null key then passing it to Dapper.
Related errors
AI-assisted analysis of DapperLib/Dapper@72a54c475f (2026-08-13).
Data as JSON: /api/errors/5e756ec046260a8f.
Report an issue: GitHub.