{"record":{"id":"a31f254fcec6e1ef","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-context-a31f25","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'context')","messagePattern":"Value cannot be null\\. \\(Parameter 'context'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive.Analyzers/Analyzers/AddUiFrameworkPackageAnalyzer.cs","lineNumber":136,"sourceCode":"            PackagingCategory,\n            DiagnosticSeverity.Warning,\n            isEnabledByDefault: true,\n            description: ReferenceToRxUwpRequiredDescription,\n            helpLinkUri: \"https://github.com/dotnet/reactive\");\n\n        /// <inheritdoc/>\n        public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(\n        [\n            ReferenceToRxWindowsFormsRequiredRule,\n            ReferenceToRxWpfRequiredRule,\n            ReferenceToRxWindowsRuntimeRequiredRule,\n            ReferenceToRxUwpRequiredRule\n        ]);\n\n        /// <inheritdoc/>\n        public override void Initialize(AnalysisContext context)\n        {\n            if (context is null) {  throw new ArgumentNullException(nameof(context)); }\n\n            context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);\n            context.EnableConcurrentExecution();\n\n            context.RegisterSemanticModelAction(AnalyzeSemanticModel);\n        }\n\n        private void AnalyzeSemanticModel(SemanticModelAnalysisContext context)\n        {\n            // Note: our goal is to do as little work as possible in cases where we won't produce a\n            // diagnostic. We expect not to need to report anything the majority of the time, so we\n            // want our impact to be minimal.\n            //\n            // We have registered for this callback, and not for syntax node ones, because we only\n            // want to run when there are errors.\n            var d = context.SemanticModel.GetDiagnostics();\n            foreach (var diag in d)\n            {","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive.Analyzers/Analyzers/AddUiFrameworkPackageAnalyzer.cs#L118-L154","documentation":"AddUiFrameworkPackageAnalyzer.Initialize throws ArgumentNullException because its AnalysisContext parameter was null. Roslyn analyzers receive the AnalysisContext from the compiler host during analyzer registration; the library guards explicitly so a misconfigured host or direct unit-test invocation fails fast instead of crashing later during ConfigureGeneratedCodeAnalysis or RegisterSemanticModelAction.","triggerScenarios":"Calling AddUiFrameworkPackageAnalyzer.Initialize(null) directly, or a Roslyn analysis engine / test harness that supplies a null AnalysisContext when instantiating the analyzer.","commonSituations":"Custom analyzer test runners that forget to pass a context, mocking frameworks returning null from AnalysisContext factories, or embedding Roslyn analyzers in a non-standard tool host.","solutions":["Ensure the analyzer is executed through a Roslyn host (Visual Studio, MSBuild/csc /analyzer, or Microsoft.CodeAnalysis testing packages) that supplies a valid AnalysisContext.","In custom test harnesses, construct a real AnalysisContext (e.g. via AnalyzerOptions and a compilation start context from the Roslyn testing framework) rather than passing null.","If you are hosting analyzers yourself, verify the CodeAnalysis runtime version matches the analyzer's target Roslyn version so context creation is not skipped."],"exampleFix":"// before\nnew AddUiFrameworkPackageAnalyzer().Initialize(null);\n// after\n// Run via a real Roslyn session:\nvar compilation = CSharpCompilation.Create(\"proj\", syntaxTrees);\ncompilation.WithAnalyzers(ImmutableArray.Create<DiagnosticAnalyzer>(new AddUiFrameworkPackageAnalyzer()));","handlingStrategy":"validation","validationCode":"if (context == null) throw new ArgumentNullException(nameof(context)); // or only call Initialize via a real Roslyn host\nanalyzer.Initialize(context);","typeGuard":"static bool HasContext(AnalysisContext c) => c is not null;","tryCatchPattern":"try\n{\n    analyzer.Initialize(context);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"context\")\n{\n    // log: analyzer invoked without a Roslyn context; fix host\n}","preventionTips":["Run analyzers only through a Roslyn host (VS, MSBuild, csc /analyzer) or the official Microsoft.CodeAnalysis testing packages.","Never hand-roll AnalysisContext instances; use Compilation.WithAnalyzers in tests.","Keep the analyzer's Roslyn package version aligned with the host's Microsoft.CodeAnalysis version."],"tags":["roslyn","analyzer","null-argument"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}