JamesNK/Newtonsoft.Json · error · ArgumentNullException
{parameterName}
Error message
{parameterName} What it means
The central null-argument guard used across the entire Newtonsoft.Json API surface (Src/Newtonsoft.Json/Utilities/ValidationUtils.cs:34-40). ValidationUtils.ArgumentNotNull(value, parameterName) throws an ArgumentNullException whose ParamName and message equal the supplied parameterName whenever value is null. Dozens of public entry points funnel through it - JsonConvert.DeserializeObject (JsonConvert.cs:857), JsonSerializer.Serialize/Deserialize with null readers/writers/targets (JsonSerializer.cs:811,890,1091,1204), JsonWriter.WriteFromReader (JsonWriter.cs:511+), converters (StringEnumConverter.cs:134), contract resolvers, reflection value providers, and formatter converters - so this is the library's standard 'you passed null where a non-null argument is required' signal. The thrown message is literally the parameter name (e.g. 'value', 'reader', 'type'), which tells you exactly which argument was null.
Source
Thrown at Src/Newtonsoft.Json/Utilities/ValidationUtils.cs:38
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace Newtonsoft.Json.Utilities
{
internal static class ValidationUtils
{
public static void ArgumentNotNull([NotNull]object? value, string parameterName)
{
if (value == null)
{
throw new ArgumentNullException(parameterName);
}
}
}
}View on GitHub (pinned to 4f73e74372)
Solutions
- Null-check the argument before calling and handle the absent value (return early, use a default, or log): if (jsonString is null) return null;
- Switch to an overload that tolerates null for that argument where one exists (some SerializeObject overloads accept null values; settings and Type arguments generally do not).
- Enable <Nullable>enable</Nullable> and treat nullable warnings as errors so null arguments are caught at compile time.
- Ensure the source feeding the JSON (HTTP client, file read, configuration provider) returns a non-null string by coalescing at the boundary.
Example fix
// before var obj = JsonConvert.DeserializeObject<MyType>(jsonString); // jsonString may be null -> ArgumentNullException (ParamName="value") // after if (jsonString is null) return null; var obj = JsonConvert.DeserializeObject<MyType>(jsonString);
Defensive patterns
Strategy: validation
Validate before calling
if (jsonString is null)
return null; // or: throw your own domain-specific exception
var obj = JsonConvert.DeserializeObject<MyType>(jsonString); Type guard
static bool HasJson([NotNullWhen(true)] string? s) => !string.IsNullOrEmpty(s);
// usage
if (HasJson(jsonString))
Deserialize(jsonString); Try / catch
try
{
var obj = JsonConvert.DeserializeObject<MyType>(jsonString);
}
catch (ArgumentNullException ex) when (ex.ParamName == "value")
{
// jsonString (or the named argument) was null; handle the missing-input case.
} Prevention
- Enable <Nullable>enable</Nullable> and treat nullable warnings as errors to catch null arguments at compile time.
- Coalesce nulls at the boundary (jsonString ?? string.Empty) or short-circuit before calling the serializer.
- Read the ArgumentNullException.ParamName to identify exactly which argument was null.
- Catch ArgumentNullException only when you can genuinely recover; otherwise fix the null at its source (HTTP/file/config reader).
When it happens
Trigger: Passing null to any guarded public API: JsonConvert.DeserializeObject(null, ...), JsonSerializer.Deserialize with a null JsonReader, JsonSerializer.Serialize with a null JsonWriter, a null Type/objectType to DefaultContractResolver.ResolveContract or JsonContract, null value/reader/writer in converter and formatter code (FormatterConverter.cs, JsonFormatterConverter.cs), null memberInfo to value providers, null creator in JsonSerializerInternalReader, etc. The ArgumentNullException.ParamName identifies the offending argument.
Common situations: Deserializing from a JSON string that came back null from an HTTP response, config file, or DB read; passing a null TextReader/JsonReader; nullable reference types disabled so the compiler never warned; a refactor that introduced null where a non-null overload was expected; null JsonSerializerSettings.TypeNameHandling-related type arguments.
Related errors
- No object created.
- Unexpected merge array handling when merging JSON.
- arrayIndex is less than 0.
- arrayIndex is equal to or greater than the length of array.
- The number of elements in the source JObject is greater than
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/435e86038a0bccd7.
Report an issue: GitHub.