{"record":{"id":"8b0ffd1499e8f8d3","repo":"JamesNK/Newtonsoft.Json","slug":"could-not-determine-new-value-to-add-to-0","errorCode":null,"errorMessage":"Could not determine new value to add to '{0}'.","messagePattern":"Could not determine new value to add to '(.+?)'\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JContainer.cs","lineNumber":1132,"sourceCode":"                return _syncRoot;\n            }\n        }\n        #endregion\n\n        #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","sourceCodeStart":1114,"sourceCodeEnd":1150,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JContainer.cs#L1114-L1150","documentation":"Thrown by IBindingList.AddNew on a JContainer when AddingNewEventArgs.NewObject is null after raising the AddingNew event. AddNew expects an event handler to supply the new object; if no handler is attached or the handler leaves NewObject null, Newtonsoft.Json cannot construct a value and throws JsonException.","triggerScenarios":"Calling ((IBindingList)container).AddNew() (directly or via BindingSource.AddNew on a JObject/JArray) without subscribing to the AddingNew event, or with a handler that does not set e.NewObject to a JToken instance.","commonSituations":"Using a JContainer as a bindable list in WinForms/WPF and relying on default 'add new' behavior; a BindingSource whose AddingNew handler was removed or never wired; UI that allows the user to add rows.","solutions":["Subscribe to the container's AddingNew event and set e.NewObject to a new JToken (e.g. new JObject() or new JValue(0)).","Do not rely on IBindingList.AddNew for JContainer; create and Add the JToken explicitly.","If binding through BindingSource, handle its AddingNew event instead and supply the object."],"exampleFix":"// before\n((IBindingList)jArray).AddNew(); // JsonException: cannot determine new value\n\n// after\njArray.AddingNew += (s, e) => e.NewObject = new JObject();\n((IBindingList)jArray).AddNew();","handlingStrategy":"validation","validationCode":"// Ensure an AddingNew handler always supplies a JToken before enabling AddNew.\njArray.AddingNew += (s, e) =>\n{\n    e.NewObject ??= new JObject();\n};","typeGuard":null,"tryCatchPattern":"try { ((IBindingList)container).AddNew(); }\ncatch (JsonException ex) when (ex.Message.Contains(\"Could not determine new value\"))\n{\n    var token = new JObject();\n    container.Add(token);\n}","preventionTips":["Always subscribe to AddingNew before binding a JContainer to a BindingSource.","Do not expose AddNew to users unless you can supply a default JToken.","Prefer building and Add-ing JTokens explicitly over AddNew."],"tags":["newtonsoft-json","linq-to-json","bindinglist","data-binding","winforms"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}