{"record":{"id":"d83726fecb6b7666","repo":"JamesNK/Newtonsoft.Json","slug":"key","errorCode":null,"errorMessage":"key","messagePattern":"key","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JPropertyKeyedCollection.cs","lineNumber":84,"sourceCode":"                if (keyForItem != null)\n                {\n                    RemoveKey(keyForItem);\n                }\n            }\n        }\n\n        protected override void ClearItems()\n        {\n            base.ClearItems();\n\n            _dictionary?.Clear();\n        }\n\n        public bool Contains(string key)\n        {\n            if (key == null)\n            {\n                throw new ArgumentNullException(nameof(key));\n            }\n\n            if (_dictionary != null)\n            {\n                return _dictionary.ContainsKey(key);\n            }\n\n            return false;\n        }\n\n        private bool ContainsItem(JToken item)\n        {\n            if (_dictionary == null)\n            {\n                return false;\n            }\n\n            string key = GetKeyForItem(item);","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JPropertyKeyedCollection.cs#L66-L102","documentation":"JPropertyKeyedCollection.Contains(string key) throws ArgumentNullException 'key' (JPropertyKeyedCollection.cs:84) when the supplied property name is null. JObject lookups are keyed by non-null strings, so passing null — usually an uninitialized or missing variable — is rejected at the boundary.","triggerScenarios":"Calling a JObject membership check (e.g. obj.ContainsKey(name), obj.Property(name)) with a null name argument — commonly a variable that came back null from config, a dictionary miss, or a missing JSON field used as a key.","commonSituations":"Using a downstream/derived string as a property key without null-checking it; reading a key name from configuration or another JSON field that was absent.","solutions":["Null-check the key variable before calling Contains/Property.","Trace where the null originated (config, dictionary lookup, optional field) and provide a default.","Use string.IsNullOrWhiteSpace guards at API boundaries."],"exampleFix":"// before\nstring name = GetNameFromConfig(); // may be null\nbool has = obj.ContainsKey(name); // throws ArgumentNullException\n\n// after\nstring name = GetNameFromConfig();\nbool has = name != null && obj.ContainsKey(name);","handlingStrategy":"validation","validationCode":"string name = GetKeyFromConfig();\nbool has = !string.IsNullOrEmpty(name) && obj.ContainsKey(name);","typeGuard":"static bool IsValidKey(string? key) => !string.IsNullOrEmpty(key);","tryCatchPattern":null,"preventionTips":["Null-check key variables sourced from config or other JSON fields.","Provide defaults for optional key inputs.","Guard API boundaries with IsNullOrWhiteSpace checks."],"tags":["json","linq-to-json","jobject","null","argument"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}