{"record":{"id":"2051f08aa8fed705","repo":"JamesNK/Newtonsoft.Json","slug":"cannot-pass-a-null-parameter-to-the-constructor","errorCode":null,"errorMessage":"Cannot pass a null parameter to the constructor.","messagePattern":"Cannot pass a null parameter to the constructor\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs","lineNumber":280,"sourceCode":"        private static Func<object[]?, object> GetCreator(\n            [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]\n            Type type)\n        {\n            Func<object>? defaultConstructor = (ReflectionUtils.HasDefaultConstructor(type, false))\n                ? ReflectionDelegateFactory.CreateDefaultConstructor<object>(type)\n                : null;\n\n            return (parameters) =>\n            {\n                try\n                {\n                    if (parameters != null)\n                    {\n                        Type[] paramTypes = parameters.Select(param =>\n                        {\n                            if (param == null)\n                            {\n                                throw new InvalidOperationException(\"Cannot pass a null parameter to the constructor.\");\n                            }\n\n                            return param.GetType();\n                        }).ToArray();\n                        ConstructorInfo? parameterizedConstructorInfo = type.GetConstructor(paramTypes);\n\n                        if (parameterizedConstructorInfo != null)\n                        {\n                            ObjectConstructor<object> parameterizedConstructor = ReflectionDelegateFactory.CreateParameterizedConstructor(parameterizedConstructorInfo);\n                            return parameterizedConstructor(parameters);\n                        }\n                        else\n                        {\n                            throw new JsonException(\"No matching parameterized constructor found for '{0}'.\".FormatWith(CultureInfo.InvariantCulture, type));\n                        }\n                    }\n\n                    if (defaultConstructor == null)","sourceCodeStart":262,"sourceCodeEnd":298,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs#L262-L298","documentation":"Thrown inside JsonTypeReflector.GetCreator's parameterized lambda when one of the user-supplied constructor arguments is null. Because the code infers each parameter's Type via param.GetType(), a null parameter has no type to infer, so it cannot match a constructor overload.","triggerScenarios":"Creating a JsonConverter or NamingStrategy instance via [JsonConverter(typeof(T), parameters)] or CreateJsonConverterInstance/CreateNamingStrategyInstance where one of the parameter objects is null.","commonSituations":"An attribute like [JsonConverter(typeof(MyConverter), null)] (rarely expressible but possible via programmatic attribute construction), a custom JsonConverterAttribute subclass that passes a null parameter, or code that builds converter/naming-strategy arguments dynamically and accidentally includes null.","solutions":["Provide a non-null sentinel value (e.g. string.Empty, 0) in place of null where the parameter semantically allows a default.","Add an overload of the converter/naming-strategy constructor that does not take that parameter so null is never passed.","If the null is meaningful, write a custom JsonConverterAttribute that constructs the converter directly instead of relying on GetCreator's type inference.","Audit calls to CreateJsonConverterInstance / CreateNamingStrategyInstance to ensure no null elements in the args array."],"exampleFix":"// before\n[JsonConverter(typeof(DateConverter), null)]\npublic DateTime Created { get; set; }\n// after: explicit overload or sentinel\n[JsonConverter(typeof(DateConverter))]\npublic DateTime Created { get; set; }\npublic class DateConverter : JsonConverter { public DateConverter() {} ... }","handlingStrategy":"validation","validationCode":"if (args != null && args.Any(a => a == null)) throw new ArgumentException(\"null args not supported by GetCreator\");","typeGuard":"static bool HasNoNullArgs(object[] args) => args == null || args.All(a => a != null);","tryCatchPattern":"try { JsonTypeReflector.CreateJsonConverterInstance(type, args); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Cannot pass a null parameter\")) {\n    logger.Error(ex, \"null constructor arg supplied to converter; supply a non-null value or add an overload.\"); throw;\n}","preventionTips":["Never pass null elements in [JsonConverter(typeof(T), args)] arrays.","Add parameterless or sentinel-based constructor overloads on custom converters.","Construct converter instances manually and add to settings.Converters when nulls are needed.","Unit-test converter construction with the exact attribute arguments."],"tags":["converter","naming-strategy","reflection","constructor"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}