git-ecosystem/git-credential-manager · error · ArgumentNullException

State value cannot be null.

Error message

State value cannot be null.

What it means

This ArgumentNullException is raised by the validation helper GitStateValidation.ValidateValue (src/Core/GitStateValidation.cs:113), a generic guard called before any state entry value is stored or emitted. It fires when a caller passes a null string as the state entry value (the 'value' parameter). State values must be non-null so the serialized state payload stays well-formed; passing null indicates the caller failed to initialize or load the state value before persisting it.

Solutions

  1. Initialize the state value to a non-null default (e.g. empty string) before calling ValidateValue or writing the state entry.
  2. Skip persisting state entries whose value is null instead of passing them to the validator.
  3. Fail fast upstream with a clearer, domain-specific error that names the missing state field.
  4. Add a null check or null-forgiving/guard logic at the call site so null values never reach ValidateValue.

Example fix

Ensure the state value is initialized before storing it: replace null with an empty string or a meaningful default, or skip the entry entirely. Example: if (value != null) { GitStateValidation.ValidateValue(value); state[key] = value; } — or throw a domain-specific error explaining which state field is missing.
Defensive patterns

Strategy: validation

Validate before calling

if (value is null) return; // skip state entry, or use string.Empty

Type guard

static bool IsValidStateValue(string v) => v is not null;

Try / catch

try { response.SetState(key, value); }
catch (ArgumentNullException) { /* omit this state entry */ }

Prevention

When it happens

Trigger: A caller invokes ValidateValue (directly or via state-entry write paths) with a state value that is null because it was never initialized, an optional lookup returned null, or a deserialization step produced a null value.

Common situations: Persisting Git credential-manager state entries where the value was not yet computed; copying state dictionaries where a key maps to null; constructing state payloads from configuration fields that are absent; tests exercising ValidateValue with null input.

Related errors


AI-assisted analysis of git-ecosystem/git-credential-manager@e8ce762cd0 (2026-09-11). Data as JSON: /api/errors/c94705d15746c1fc. Report an issue: GitHub.

Appendix: source

Thrown at src/Core/GitStateValidation.cs:113

        if (key.StartsWith(Constants.CredentialProtocol.GcmStatePrefix, StringComparison.Ordinal))
        {
            throw new ArgumentException(
                $"State key cannot start with '{Constants.CredentialProtocol.GcmStatePrefix}'; " +
                "the prefix is reserved and added automatically when state is emitted.",
                nameof(key));
        }
    }

    /// <summary>
    /// Throws <see cref="ArgumentException"/> if <paramref name="value"/> is
    /// not a legal state entry value.
    /// </summary>
    public static void ValidateValue(string value)
    {
        if (value is null)
        {
            throw new ArgumentNullException(nameof(value), "State value cannot be null.");
        }

        foreach (char c in value)
        {
            if (c == LF || c == NUL)
            {
                throw new ArgumentException(
                    "State value cannot contain newline or NUL characters.",
                    nameof(value));
            }
        }
    }
}

View on GitHub (pinned to e8ce762cd0)