JamesNK/Newtonsoft.Json · error · ArgumentNullException

type

Error message

type

What it means

ArgumentNullException(nameof(type)) from JToken.Annotation(Type type), the non-generic overload that retrieves the first annotation assignable to the given Type. The lookup walks the annotation list using type.IsInstanceOfType, so a null Type would NullReferenceException deep inside; instead it is rejected at the boundary. Use the generic Annotation<T>() overload to avoid passing a Type object at all.

Source

Thrown at Src/Newtonsoft.Json/Linq/JToken.cs:2623

                    {
                        return local;
                    }
                }
            }

            return default;
        }

        /// <summary>
        /// Gets the first annotation object of the specified type from this <see cref="JToken"/>.
        /// </summary>
        /// <param name="type">The <see cref="Type"/> of the annotation to retrieve.</param>
        /// <returns>The first annotation object that matches the specified type, or <c>null</c> if no annotation is of the specified type.</returns>
        public object? Annotation(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (_annotations != null)
            {
                if (!(_annotations is object[] annotations))
                {
                    if (type.IsInstanceOfType(_annotations))
                    {
                        return _annotations;
                    }
                }
                else
                {
                    for (int i = 0; i < annotations.Length; i++)
                    {
                        object o = annotations[i];
                        if (o == null)
                        {

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Null-check the Type argument before calling: if (type != null) token.Annotation(type);
  2. Prefer the generic Annotation<T>() overload where the type is compile-time known.
  3. Validate the type was resolved successfully upstream (the reflection result is non-null) before using it.

Example fix

// before
var ann = token.Annotation(someType);

// after
var ann = someType != null ? token.Annotation(someType) : null;
Defensive patterns

Strategy: validation

Validate before calling

if (type != null) { var ann = token.Annotation(type); }

Type guard

static bool IsUsableType(Type? t) => t != null;

Try / catch

try { var a = token.Annotation(type); } catch (ArgumentNullException) { /* null type, skip */ }

Prevention

When it happens

Trigger: Calling token.Annotation((Type)null) explicitly, or passing a type variable that was never assigned (e.g. from a reflection call that returned null for an unloaded type).

Common situations: Generic helpers that accept Type? parameters and forward them without null-checking; reflection-driven code that resolves a type that may be missing.

Related errors


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/759d8b315036aa32. Report an issue: GitHub.