{"record":{"id":"38f2fc7ed8c9b2bf","repo":"devlooped/moq","slug":"value-cannot-be-null-parameter-newexpression","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'newExpression')","messagePattern":"Value cannot be null\\. \\(Parameter 'newExpression'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Moq/Expressions/Visitors/ConstructorCallVisitor.cs","lineNumber":26,"sourceCode":"using System.Linq.Expressions;\nusing System.Reflection;\n\nusing Moq.Properties;\n\nnamespace Moq.Expressions.Visitors\n{\n    class ConstructorCallVisitor : ExpressionVisitor\n    {\n        /// <summary>\n        /// Extracts the arguments from a lambda expression that calls a constructor.\n        /// </summary>\n        /// <param name=\"newExpression\">The constructor expression.</param>\n        /// <returns>Extracted argument values.</returns>\n        public static object[] ExtractArgumentValues(LambdaExpression newExpression)\n        {\n            if (newExpression is null)\n            {\n                throw new ArgumentNullException(nameof(newExpression));\n            }\n\n            var visitor = new ConstructorCallVisitor();\n            visitor.Visit(newExpression);\n\n            if (visitor.constructor == null)\n            {\n                throw new NotSupportedException(Resources.NoConstructorCallFound);\n            }\n\n            return visitor.arguments;\n        }\n\n        ConstructorInfo? constructor;\n        object[] arguments;\n\n#if NULLABLE_REFERENCE_TYPES\n        [return: NotNullIfNotNull(\"node\")]","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Expressions/Visitors/ConstructorCallVisitor.cs#L8-L44","documentation":"ConstructorCallVisitor.ExtractArgumentValues extracts constructor arguments from a NewExpression-based lambda. It requires a lambda; passing null throws ArgumentNullException for 'newExpression'. This is an explicit API-contract guard so callers fail fast rather than getting a NullReferenceException inside the visitor.","triggerScenarios":"Calling ExtractArgumentValues(null) directly, or passing the result of an expression-producing helper that returned null (failed resolution, default(T) of a LambdaExpression variable, unassigned field).","commonSituations":"Custom value providers/fixture integrations that build lambda expressions and can return null; reflection-driven test frameworks passing through unbound values; refactors where an expression variable lost its initializer.","solutions":["Ensure the lambda expression is constructed before the call, e.g. pass a real `Expression.Lambda(...)` or a typed lambda argument.","Null-check the expression at the call site and fail with a descriptive error identifying the source.","Fix the upstream producer (fixture/factory) that returns null instead of a LambdaExpression.","If the value is optional, branch before calling ExtractArgumentValues rather than relying on it to tolerate null."],"exampleFix":"// before\nvar args = ConstructorCallVisitor.ExtractArgumentValues(_cachedExpression); // null\n// after\nif (_cachedExpression != null)\n    var args = ConstructorCallVisitor.ExtractArgumentValues(_cachedExpression);","handlingStrategy":"validation","validationCode":"if (newExpression == null)\n    throw new InvalidOperationException(\"Lambda for constructor extraction was not built\");\nvar args = ConstructorCallVisitor.ExtractArgumentValues(newExpression);","typeGuard":"static bool IsUsableLambda(LambdaExpression? e) => e is not null && e.Body is not null;","tryCatchPattern":"try\n{\n    var values = ConstructorCallVisitor.ExtractArgumentValues(lambda);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"newExpression\")\n{\n    // rebuild or source the lambda from the caller\n}","preventionTips":["Null-check expression-producing helpers before passing results on","Avoid default(T) for LambdaExpression variables","Fail fast in factories: return a descriptive error, not a null expression","Assert expression availability in test setup code"],"tags":["moq","null-argument","expression-visitor","constructor"],"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"}