JamesNK/Newtonsoft.Json · error · InvalidOperationException
Cannot pass a null parameter to the constructor.
Error message
Cannot pass a null parameter to the constructor.
What it means
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.
Source
Thrown at Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs:280
private static Func<object[]?, object> GetCreator(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
Type type)
{
Func<object>? defaultConstructor = (ReflectionUtils.HasDefaultConstructor(type, false))
? ReflectionDelegateFactory.CreateDefaultConstructor<object>(type)
: null;
return (parameters) =>
{
try
{
if (parameters != null)
{
Type[] paramTypes = parameters.Select(param =>
{
if (param == null)
{
throw new InvalidOperationException("Cannot pass a null parameter to the constructor.");
}
return param.GetType();
}).ToArray();
ConstructorInfo? parameterizedConstructorInfo = type.GetConstructor(paramTypes);
if (parameterizedConstructorInfo != null)
{
ObjectConstructor<object> parameterizedConstructor = ReflectionDelegateFactory.CreateParameterizedConstructor(parameterizedConstructorInfo);
return parameterizedConstructor(parameters);
}
else
{
throw new JsonException("No matching parameterized constructor found for '{0}'.".FormatWith(CultureInfo.InvariantCulture, type));
}
}
if (defaultConstructor == null)View on GitHub (pinned to 4f73e74372)
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.
Example fix
// before
[JsonConverter(typeof(DateConverter), null)]
public DateTime Created { get; set; }
// after: explicit overload or sentinel
[JsonConverter(typeof(DateConverter))]
public DateTime Created { get; set; }
public class DateConverter : JsonConverter { public DateConverter() {} ... } Defensive patterns
Strategy: validation
Validate before calling
if (args != null && args.Any(a => a == null)) throw new ArgumentException("null args not supported by GetCreator"); Type guard
static bool HasNoNullArgs(object[] args) => args == null || args.All(a => a != null);
Try / catch
try { JsonTypeReflector.CreateJsonConverterInstance(type, args); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Cannot pass a null parameter")) {
logger.Error(ex, "null constructor arg supplied to converter; supply a non-null value or add an overload."); throw;
} Prevention
- 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.
When it happens
Trigger: Creating a JsonConverter or NamingStrategy instance via [JsonConverter(typeof(T), parameters)] or CreateJsonConverterInstance/CreateNamingStrategyInstance where one of the parameter objects is null.
Common situations: 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.
Related errors
- No matching parameterized constructor found for '{0}'.
- No parameterless constructor defined for '{0}'.
- Error creating '{0}'.
- Could not get constructor for {0}.
- Unable to find default constructor for
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/2051f08aa8fed705.
Report an issue: GitHub.