dotnet/BenchmarkDotNet · error · ArgumentNullException

Value cannot be null. (Parameter 'value')

Error message

Value cannot be null. (Parameter 'value')

What it means

Thrown by the EnvironmentVariable constructor when the 'value' parameter is null. The constructor requires both key and value to be non-null because the value is set on the host process environment and rendered in benchmark reports. A null value would cause a NullReferenceException when the variable is passed to the process or formatted via ToString().

Source

Thrown at src/BenchmarkDotNet/Jobs/EnvironmentVariable.cs:8

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))
                return false;

View on GitHub (pinned to b515068b61)

Solutions

  1. Coalesce null values to empty string if semantically acceptable: value ?? string.Empty.
  2. Filter out entries with null values before constructing EnvironmentVariable objects.
  3. Validate the data source ensures every key has a corresponding non-null value.

Example fix

// before
var envVar = new EnvironmentVariable(entry.Key, entry.Value);

// after
var envVar = new EnvironmentVariable(entry.Key, entry.Value ?? string.Empty);
Defensive patterns

Strategy: validation

Validate before calling

if (value is null)
    value = string.Empty; // or throw, or skip
var envVar = new EnvironmentVariable(key, value);

Type guard

static bool IsValidEnvVarValue(string? value) => value is not null;

Prevention

When it happens

Trigger: Constructing new EnvironmentVariable("KEY", null) or passing a null value that originated from a missing dictionary entry, nullable config field, or a LINQ Select that produced null.

Common situations: Loading environment variables from configuration where some entries have keys but missing/null values. Also occurs when conditionally setting variables and forgetting to provide the value branch.

Related errors


AI-assisted analysis of dotnet/BenchmarkDotNet@b515068b61 (2026-08-13). Data as JSON: /api/errors/4e4cf08e1684d6d9. Report an issue: GitHub.