{"record":{"id":"a87638a89807412f","repo":"devlooped/moq","slug":"value-cannot-be-null-parameter-value","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'value')","messagePattern":"Value cannot be null\\. \\(Parameter 'value'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Moq/ExpressionCompiler.cs","lineNumber":31,"sourceCode":"    /// </summary>\n    [EditorBrowsable(EditorBrowsableState.Advanced)]\n    public abstract class ExpressionCompiler\n    {\n        static ExpressionCompiler instance = DefaultExpressionCompiler.Instance;\n\n        /// <summary>\n        ///   The default <see cref=\"ExpressionCompiler\"/> instance, which simply delegates to the framework's <see cref=\"LambdaExpression.Compile\"/>.\n        /// </summary>\n        public static ExpressionCompiler Default => DefaultExpressionCompiler.Instance;\n\n        /// <summary>\n        ///   Gets or sets the <see cref=\"ExpressionCompiler\"/> instance that Moq uses to compile <see cref=\"Expression\"/> (LINQ expression trees).\n        ///   Defaults to <see cref=\"Default\"/>.\n        /// </summary>\n        public static ExpressionCompiler Instance\n        {\n            get => instance;\n            set => instance = value ?? throw new ArgumentNullException(nameof(value));\n        }\n\n        /// <summary>\n        ///   Initializes a new instance of the <see cref=\"ExpressionCompiler\"/> class.\n        /// </summary>\n        protected ExpressionCompiler()\n        {\n        }\n\n        /// <summary>\n        ///   Compiles the specified LINQ expression tree.\n        /// </summary>\n        /// <param name=\"expression\">The LINQ expression tree that should be compiled.</param>\n        public abstract Delegate Compile(LambdaExpression expression);\n\n        /// <summary>\n        ///   Compiles the specified LINQ expression tree.\n        /// </summary>","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/ExpressionCompiler.cs#L13-L49","documentation":"Moq allows replacing the expression compiler it uses via the static ExpressionCompiler.Instance property. The setter guards the library's invariant that a compiler must always be present; assigning null would break every later mock that compiles expressions, so it throws ArgumentNullException for parameter 'value' immediately.","triggerScenarios":"Assigning null to `ExpressionCompiler.Instance`, e.g. `ExpressionCompiler.Instance = null;` or passing a variable that is null at assignment time (uninitialized factory result, failed resolution, reset code).","commonSituations":"Custom compiler factories that return null on failure; DI containers resolving the compiler to null; cleanup/reset code that tries to 'clear' the instance by assigning null; test teardown ordering issues.","solutions":["Only assign a non-null ExpressionCompiler instance; store the previous value and restore that instead of null.","Null-check the compiler obtained from your factory/DI container before assigning it to Instance.","If you want default behavior, do not assign the property at all — it already defaults to Default; there is no need to reset via null.","Wrap assignment in a guard that throws a descriptive error when the resolved compiler is null so the root cause (factory failure) surfaces."],"exampleFix":"// before\nExpressionCompiler.Instance = _container.Resolve<ExpressionCompiler>(); // may be null\n// after\nvar compiler = _container.Resolve<ExpressionCompiler>();\nif (compiler != null) ExpressionCompiler.Instance = compiler;","handlingStrategy":"validation","validationCode":"var compiler = ResolveCompiler();\nif (compiler == null) throw new InvalidOperationException(\"Resolved ExpressionCompiler was null\");\nExpressionCompiler.Instance = compiler;","typeGuard":"static bool CanSet(ExpressionCompiler? c) => c is not null;","tryCatchPattern":"try\n{\n    ExpressionCompiler.Instance = resolved;\n}\ncatch (ArgumentNullException)\n{\n    // keep previous/default instance; log factory failure\n}","preventionTips":["Never assign null to static Instance properties; restore a saved default instead","Null-check DI/factory results before assigning","Rely on the built-in Default unless you truly need a custom compiler","Centralize static override logic in one guarded place"],"tags":["moq","null-argument","static-configuration","expression-compiler"],"backgroundTag":"null-argument","analyzedSha":"89a5be629c752960fe403e95ff583e4ae6f00542","analyzedAt":"2026-09-16T05:31:39.496Z","contentChangedAt":"2026-09-16T05:31:39.496Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}