{"record":{"id":"c170c4a0b7d0e80a","repo":"JamesNK/Newtonsoft.Json","slug":"new-item-to-be-added-to-collection-must-be-compati","errorCode":null,"errorMessage":"New item to be added to collection must be compatible with {0}.","messagePattern":"New item to be added to collection must be compatible with (.+?)\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JContainer.cs","lineNumber":1137,"sourceCode":"        #region IBindingList Members\n#if HAVE_COMPONENT_MODEL\n        void IBindingList.AddIndex(PropertyDescriptor property)\n        {\n        }\n\n        object IBindingList.AddNew()\n        {\n            AddingNewEventArgs args = new AddingNewEventArgs();\n            OnAddingNew(args);\n\n            if (args.NewObject == null)\n            {\n                throw new JsonException(\"Could not determine new value to add to '{0}'.\".FormatWith(CultureInfo.InvariantCulture, GetType()));\n            }\n\n            if (!(args.NewObject is JToken newItem))\n            {\n                throw new JsonException(\"New item to be added to collection must be compatible with {0}.\".FormatWith(CultureInfo.InvariantCulture, typeof(JToken)));\n            }\n\n            Add(newItem);\n\n            return newItem;\n        }\n\n        bool IBindingList.AllowEdit => true;\n\n        bool IBindingList.AllowNew => true;\n\n        bool IBindingList.AllowRemove => true;\n\n        void IBindingList.ApplySort(PropertyDescriptor property, ListSortDirection direction)\n        {\n            throw new NotSupportedException();\n        }\n","sourceCodeStart":1119,"sourceCodeEnd":1155,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JContainer.cs#L1119-L1155","documentation":"Thrown by IBindingList.AddNew on a JContainer when the AddingNew handler did supply an object but it is not a JToken. AddNew validates that the user-provided NewObject is assignable to JToken; otherwise it throws JsonException because a JContainer can only hold JToken children.","triggerScenarios":"Subscribing to AddingNew and setting e.NewObject to a POCO, string, int, DataRow, or any non-JToken type, then triggering AddNew (directly or via BindingSource.AddNew).","commonSituations":"Handlers that return a domain model instead of a JSON node; copy-paste of an AddingNew handler from a non-JToken list; mixing typed model objects into a JObject/JArray binding surface.","solutions":["In the AddingNew handler, wrap or build a JToken: e.NewObject = JToken.FromObject(myPoco) or new JObject(new JProperty(\"x\", val)).","Return a fresh empty JObject/JArray/JValue matching the container's expected child type.","Avoid AddNew; construct the JToken yourself and call Add."],"exampleFix":"// before\njArray.AddingNew += (s, e) => e.NewObject = new MyItem(); // not a JToken\n\n// after\njArray.AddingNew += (s, e) => e.NewObject = JToken.FromObject(new MyItem());","handlingStrategy":"type-guard","validationCode":"static JToken ToJToken(object o) => o switch\n{\n    JToken t => t,\n    _ => JToken.FromObject(o)\n};\n\njArray.AddingNew += (s, e) => e.NewObject = ToJToken(CreateNewItem());","typeGuard":"static bool IsJTokenCompatible(object? o) => o is JToken;","tryCatchPattern":"try { ((IBindingList)container).AddNew(); }\ncatch (JsonException ex) when (ex.Message.Contains(\"must be compatible with\"))\n{\n    // Handler returned a non-JToken; wrap and add manually.\n    container.Add(JToken.FromObject(CreateNewItem()));\n}","preventionTips":["In AddingNew handlers, always return a JToken (use JToken.FromObject for POCOs).","Unit-test the handler by triggering AddNew.","Avoid returning domain model types from AddingNew on a JContainer."],"tags":["newtonsoft-json","linq-to-json","bindinglist","data-binding","type-mismatch"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}