dotnet/BenchmarkDotNet · error · ArgumentNullException
Value cannot be null. (Parameter 'key')
Error message
Value cannot be null. (Parameter 'key')
What it means
Thrown by the EnvironmentVariable constructor when the 'key' parameter is null. EnvironmentVariable represents a single key=value environment variable entry for a benchmark Job. The constructor enforces that both key and value are non-null because the variable is later serialized to the host process environment and displayed in reports (via ToString() which interpolates $"{Key}={Value}"). A null key would cause a NullReferenceException downstream.
Source
Thrown at src/BenchmarkDotNet/Jobs/EnvironmentVariable.cs:7
namespace BenchmarkDotNet.Jobs
{
public class EnvironmentVariable : IEquatable<EnvironmentVariable>
{
public EnvironmentVariable(string key, string value)
{
Key = key ?? throw new ArgumentNullException(nameof(key));
Value = value ?? throw new ArgumentNullException(nameof(value));
}
public string Key { get; }
public string Value { get; }
// CharacteristicPresenters call ToString(), this is why we need this override
public override string ToString() => $"{Key}={Value}";
public bool Equals(EnvironmentVariable? other)
{
return string.Equals(Key, other?.Key) && string.Equals(Value, other?.Value);
}
public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj))View on GitHub (pinned to b515068b61)
Solutions
- Validate the key before constructing: if (string.IsNullOrEmpty(key)) throw ... or skip the entry.
- Ensure the data source (config file, dictionary) always has non-null keys by schema validation.
- Use string.IsNullOrWhiteSpace check to also reject blank keys that are technically non-null but meaningless.
Example fix
// before
var envVar = new EnvironmentVariable(configKey, configValue);
// after
if (string.IsNullOrWhiteSpace(configKey))
throw new InvalidOperationException("Environment variable key is missing from config.");
var envVar = new EnvironmentVariable(configKey, configValue); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(key))
throw new InvalidOperationException("Environment variable key must be non-empty.");
var envVar = new EnvironmentVariable(key, value); Type guard
static bool IsValidEnvVarKey(string? key) => !string.IsNullOrWhiteSpace(key);
Prevention
- Validate configuration keys at the boundary where external data enters (config loading).
- Use nullable reference types to track where nulls can originate.
- Build environment variables from a validated Dictionary<string, string> rather than ad-hoc construction.
When it happens
Trigger: Constructing new EnvironmentVariable(null, "someValue") or passing a null key sourced from a dictionary lookup, config file, or dynamic code that did not supply a key string.
Common situations: Building environment variables from external configuration (JSON, YAML, command-line) where a key field is missing or whitespace. Also from LINQ projections over dictionaries where a null key leaks through.
Related errors
- Value cannot be null. (Parameter 'value')
- Property values can not contain null.
- The '{variable.Key}' environment variables is defined twice
AI-assisted analysis of dotnet/BenchmarkDotNet@b515068b61 (2026-08-13).
Data as JSON: /api/errors/7c224d8f76f5fd4d.
Report an issue: GitHub.