{"record":{"id":"759d8b315036aa32","repo":"JamesNK/Newtonsoft.Json","slug":"type","errorCode":null,"errorMessage":"type","messagePattern":"type","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JToken.cs","lineNumber":2623,"sourceCode":"                    {\n                        return local;\n                    }\n                }\n            }\n\n            return default;\n        }\n\n        /// <summary>\n        /// Gets the first annotation object of the specified type from this <see cref=\"JToken\"/>.\n        /// </summary>\n        /// <param name=\"type\">The <see cref=\"Type\"/> of the annotation to retrieve.</param>\n        /// <returns>The first annotation object that matches the specified type, or <c>null</c> if no annotation is of the specified type.</returns>\n        public object? Annotation(Type type)\n        {\n            if (type == null)\n            {\n                throw new ArgumentNullException(nameof(type));\n            }\n\n            if (_annotations != null)\n            {\n                if (!(_annotations is object[] annotations))\n                {\n                    if (type.IsInstanceOfType(_annotations))\n                    {\n                        return _annotations;\n                    }\n                }\n                else\n                {\n                    for (int i = 0; i < annotations.Length; i++)\n                    {\n                        object o = annotations[i];\n                        if (o == null)\n                        {","sourceCodeStart":2605,"sourceCodeEnd":2641,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JToken.cs#L2605-L2641","documentation":"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.","triggerScenarios":"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).","commonSituations":"Generic helpers that accept Type? parameters and forward them without null-checking; reflection-driven code that resolves a type that may be missing.","solutions":["Null-check the Type argument before calling: if (type != null) token.Annotation(type);","Prefer the generic Annotation<T>() overload where the type is compile-time known.","Validate the type was resolved successfully upstream (the reflection result is non-null) before using it."],"exampleFix":"// before\nvar ann = token.Annotation(someType);\n\n// after\nvar ann = someType != null ? token.Annotation(someType) : null;","handlingStrategy":"validation","validationCode":"if (type != null) { var ann = token.Annotation(type); }","typeGuard":"static bool IsUsableType(Type? t) => t != null;","tryCatchPattern":"try { var a = token.Annotation(type); } catch (ArgumentNullException) { /* null type, skip */ }","preventionTips":["Null-check Type arguments from reflection before annotation lookups.","Prefer the generic Annotation<T>() overload.","Validate Type.GetType / assembly load results."],"tags":["annotations","argumentnull","null-check","reflection"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}