dotnet/aspnetcore · error · ArgumentNullException

existingState

Error message

existingState

What it means

Thrown as ArgumentNullException(nameof(existingState)) by InitializeExistingState when the caller passes null for the existing-state dictionary. The library cannot restore state from a null table; the parameter is mandatory whenever initialization runs.

Source

Thrown at src/Components/Components/src/PersistentComponentState.cs:42

        List<PersistComponentStateRegistration> pauseCallbacks,
        List<RestoreComponentStateRegistration> restoringCallbacks)
    {
        _currentState = currentState;
        _registeredCallbacks = pauseCallbacks;
        _registeredRestoringCallbacks = restoringCallbacks;
    }

    internal bool PersistingState { get; set; }

    internal RestoreContext CurrentContext { get; private set; } = RestoreContext.InitialValue;

    internal void InitializeExistingState(IDictionary<string, byte[]> existingState, RestoreContext context)
    {
        if (_existingState != null)
        {
            throw new InvalidOperationException("PersistentComponentState already initialized.");
        }
        _existingState = existingState ?? throw new ArgumentNullException(nameof(existingState));
        CurrentContext = context;
    }

    /// <summary>
    /// Register a callback to persist the component state when the application is about to be paused.
    /// Registered callbacks can use this opportunity to persist their state so that it can be retrieved when the application resumes.
    /// </summary>
    /// <param name="callback">The callback to invoke when the application is being paused.</param>
    /// <returns>A subscription that can be used to unregister the callback when disposed.</returns>
    public PersistingComponentStateSubscription RegisterOnPersisting(Func<Task> callback)
        => RegisterOnPersisting(callback, null);

    /// <summary>
    /// Register a callback to persist the component state when the application is about to be paused.
    /// Registered callbacks can use this opportunity to persist their state so that it can be retrieved when the application resumes.
    /// </summary>
    /// <param name="callback">The callback to invoke when the application is being paused.</param>
    /// <param name="renderMode"></param>

View on GitHub (pinned to 3600ca084e)

Solutions

  1. Pass an empty dictionary instead of null when there is no prior state.
  2. Guard the call site: state?.Let(d => persistent.InitializeExistingState(d, ctx)).
  3. Ensure your host always produces a non-null IDictionary<string,byte[]> before invoking initialization.

Example fix

// before
persistent.InitializeExistingState(maybeNullDict, context);

// after
persistent.InitializeExistingState(maybeNullDict ?? new Dictionary<string, byte[]>(), context);
Defensive patterns

Strategy: validation

Validate before calling

if (existingState is null) throw new ArgumentNullException(nameof(existingState));
state.InitializeExistingState(existingState, context);

Try / catch

try { state.InitializeExistingState(maybeNull!, context); }
catch (ArgumentNullException ex) when (ex.ParamName == "existingState")
{ /* pass an empty dictionary instead of null */ }

Prevention

When it happens

Trigger: Calling PersistentComponentState.InitializeExistingState(existingState: null, context) directly. In normal framework use this never happens because the host always supplies a dictionary (possibly empty). It is reachable in custom hosts or tests that pass null by mistake.

Common situations: A custom host that条件ally builds the state and passes null when there is nothing to restore; a test helper that forwards an optional parameter without checking.

Related errors


AI-assisted analysis of dotnet/aspnetcore@3600ca084e (2026-08-11). Data as JSON: /api/errors/45254616cb29a780. Report an issue: GitHub.