{"record":{"id":"45254616cb29a780","repo":"dotnet/aspnetcore","slug":"existingstate","errorCode":null,"errorMessage":"existingState","messagePattern":"existingState","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Components/Components/src/PersistentComponentState.cs","lineNumber":42,"sourceCode":"        List<PersistComponentStateRegistration> pauseCallbacks,\n        List<RestoreComponentStateRegistration> restoringCallbacks)\n    {\n        _currentState = currentState;\n        _registeredCallbacks = pauseCallbacks;\n        _registeredRestoringCallbacks = restoringCallbacks;\n    }\n\n    internal bool PersistingState { get; set; }\n\n    internal RestoreContext CurrentContext { get; private set; } = RestoreContext.InitialValue;\n\n    internal void InitializeExistingState(IDictionary<string, byte[]> existingState, RestoreContext context)\n    {\n        if (_existingState != null)\n        {\n            throw new InvalidOperationException(\"PersistentComponentState already initialized.\");\n        }\n        _existingState = existingState ?? throw new ArgumentNullException(nameof(existingState));\n        CurrentContext = context;\n    }\n\n    /// <summary>\n    /// Register a callback to persist the component state when the application is about to be paused.\n    /// Registered callbacks can use this opportunity to persist their state so that it can be retrieved when the application resumes.\n    /// </summary>\n    /// <param name=\"callback\">The callback to invoke when the application is being paused.</param>\n    /// <returns>A subscription that can be used to unregister the callback when disposed.</returns>\n    public PersistingComponentStateSubscription RegisterOnPersisting(Func<Task> callback)\n        => RegisterOnPersisting(callback, null);\n\n    /// <summary>\n    /// Register a callback to persist the component state when the application is about to be paused.\n    /// Registered callbacks can use this opportunity to persist their state so that it can be retrieved when the application resumes.\n    /// </summary>\n    /// <param name=\"callback\">The callback to invoke when the application is being paused.</param>\n    /// <param name=\"renderMode\"></param>","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/Components/Components/src/PersistentComponentState.cs#L24-L60","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass an empty dictionary instead of null when there is no prior state.","Guard the call site: state?.Let(d => persistent.InitializeExistingState(d, ctx)).","Ensure your host always produces a non-null IDictionary<string,byte[]> before invoking initialization."],"exampleFix":"// before\npersistent.InitializeExistingState(maybeNullDict, context);\n\n// after\npersistent.InitializeExistingState(maybeNullDict ?? new Dictionary<string, byte[]>(), context);","handlingStrategy":"validation","validationCode":"if (existingState is null) throw new ArgumentNullException(nameof(existingState));\nstate.InitializeExistingState(existingState, context);","typeGuard":null,"tryCatchPattern":"try { state.InitializeExistingState(maybeNull!, context); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"existingState\")\n{ /* pass an empty dictionary instead of null */ }","preventionTips":["Always pass an empty dictionary when there is no prior state, never null.","Guard optional upstream sources: existingState ?? new Dictionary<string, byte[]>()."],"tags":["blazor","state","persistence","null-reference"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}