JamesNK/Newtonsoft.Json · error · JsonException
Could not determine new value to add to '{0}'.
Error message
Could not determine new value to add to '{0}'. What it means
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.
Source
Thrown at Src/Newtonsoft.Json/Linq/JContainer.cs:1132
return _syncRoot;
}
}
#endregion
#region IBindingList Members
#if HAVE_COMPONENT_MODEL
void IBindingList.AddIndex(PropertyDescriptor property)
{
}
object IBindingList.AddNew()
{
AddingNewEventArgs args = new AddingNewEventArgs();
OnAddingNew(args);
if (args.NewObject == null)
{
throw new JsonException("Could not determine new value to add to '{0}'.".FormatWith(CultureInfo.InvariantCulture, GetType()));
}
if (!(args.NewObject is JToken newItem))
{
throw new JsonException("New item to be added to collection must be compatible with {0}.".FormatWith(CultureInfo.InvariantCulture, typeof(JToken)));
}
Add(newItem);
return newItem;
}
bool IBindingList.AllowEdit => true;
bool IBindingList.AllowNew => true;
bool IBindingList.AllowRemove => true;
View on GitHub (pinned to 4f73e74372)
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.
Example fix
// before ((IBindingList)jArray).AddNew(); // JsonException: cannot determine new value // after jArray.AddingNew += (s, e) => e.NewObject = new JObject(); ((IBindingList)jArray).AddNew();
Defensive patterns
Strategy: validation
Validate before calling
// Ensure an AddingNew handler always supplies a JToken before enabling AddNew.
jArray.AddingNew += (s, e) =>
{
e.NewObject ??= new JObject();
}; Try / catch
try { ((IBindingList)container).AddNew(); }
catch (JsonException ex) when (ex.Message.Contains("Could not determine new value"))
{
var token = new JObject();
container.Add(token);
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- New item to be added to collection must be compatible with {
- Newtonsoft.Json support for System.ComponentModel is not com
- Argument is not a JToken.
- Unexpected merge array handling when merging JSON.
- Can not add {0} to {1}.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/8b0ffd1499e8f8d3.
Report an issue: GitHub.