{"record":{"id":"0477cdf2a5e7fa17","repo":"JamesNK/Newtonsoft.Json","slug":"array","errorCode":null,"errorMessage":"array","messagePattern":"array","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JContainer.cs","lineNumber":593,"sourceCode":"            if (existing == null || existing.Parent != this)\n            {\n                return;\n            }\n\n            int index = IndexOfItem(existing);\n            SetItem(index, replacement);\n        }\n\n        internal virtual bool ContainsItem(JToken? item)\n        {\n            return (IndexOfItem(item) != -1);\n        }\n\n        internal virtual void CopyItemsTo(Array array, int arrayIndex)\n        {\n            if (array == null)\n            {\n                throw new ArgumentNullException(nameof(array));\n            }\n            if (arrayIndex < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(arrayIndex), \"arrayIndex is less than 0.\");\n            }\n            if (arrayIndex >= array.Length && arrayIndex != 0)\n            {\n                throw new ArgumentException(\"arrayIndex is equal to or greater than the length of array.\");\n            }\n            if (Count > array.Length - arrayIndex)\n            {\n                throw new ArgumentException(\"The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.\");\n            }\n\n            int index = 0;\n            foreach (JToken token in ChildrenTokens)\n            {\n                array.SetValue(token, arrayIndex + index);","sourceCodeStart":575,"sourceCodeEnd":611,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JContainer.cs#L575-L611","documentation":"CopyItemsTo (which backs CopyTo) throws ArgumentNullException named 'array' when the destination array is null. The null-array guard runs before any bounds checks.","triggerScenarios":"jArray.CopyTo(null, 0); ((ICollection)container).CopyTo(null, 0); passing a null array variable.","commonSituations":"Uninitialized destination array; generic collection-copy helper that forwards a null.","solutions":["Allocate the destination array before copying: new JToken[container.Count].","Null-check the array argument at the call site and fail with a clear message.","Use the ToList()/LINQ materialization if you just need a snapshot."],"exampleFix":"// before\narr.CopyTo(null, 0);\n// after\nvar dest = new JToken[arr.Count];\narr.CopyTo(dest, 0);","handlingStrategy":"validation","validationCode":"var dest = array ?? new JToken[container.Count];\ncontainer.CopyTo(dest, 0);","typeGuard":null,"tryCatchPattern":"try { container.CopyTo(dest, arrayIndex); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"array\") {\n    dest = new JToken[container.Count];\n    container.CopyTo(dest, 0);\n}","preventionTips":["Allocate the destination array before calling CopyTo.","Null-check array arguments at the call site.","Use ToList() for a quick snapshot when you don't need an array."],"tags":["jcontainer","copyto","null","argument"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}