{"record":{"id":"e4a9de5aa39844f1","repo":"JamesNK/Newtonsoft.Json","slug":"error-getting-value-from-0-on-1","errorCode":null,"errorMessage":"Error getting value from '{0}' on '{1}'.","messagePattern":"Error getting value from '(.+?)' on '(.+?)'\\.","errorType":"exception","errorClass":"JsonSerializationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Serialization/DynamicValueProvider.cs","lineNumber":109,"sourceCode":"        /// <summary>\n        /// Gets the value.\n        /// </summary>\n        /// <param name=\"target\">The target to get the value from.</param>\n        /// <returns>The value.</returns>\n        public object? GetValue(object target)\n        {\n            try\n            {\n                if (_getter == null)\n                {\n                    _getter = DynamicReflectionDelegateFactory.Instance.CreateGet<object>(_memberInfo);\n                }\n\n                return _getter(target);\n            }\n            catch (Exception ex)\n            {\n                throw new JsonSerializationException(\"Error getting value from '{0}' on '{1}'.\".FormatWith(CultureInfo.InvariantCulture, _memberInfo.Name, target.GetType()), ex);\n            }\n        }\n    }\n}\n\n#endif","sourceCodeStart":91,"sourceCodeEnd":115,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Serialization/DynamicValueProvider.cs#L91-L115","documentation":"Thrown by DynamicValueProvider.GetValue when the compiled dynamic-method getter throws while reading a member value from the target object during serialization. '{0}' is the member name, '{1}' the target runtime type. The underlying exception is attached as InnerException for diagnosis.","triggerScenarios":"Serializing an object whose property getter throws (e.g. throws NullReferenceException because it dereferences a null sub-object, or throws NotSupportedException). Raised by JsonSerializer.Serialize when the IValueProvider.GetValue call for a member fails.","commonSituations":"A getter that lazily initializes from a disposed/closed resource (e.g. DataContext after dispose), a computed property that throws when a dependency is null, proxy/entity properties in EF that throw LazyLoadingException after the context is disposed, or a member that returns an incompatible runtime type.","solutions":["Inspect InnerException to identify the true getter failure.","Guard the getter against null dependencies or wrap its body so it doesn't throw during serialization.","Exclude the throwing property from serialization with [JsonIgnore] or NullValueHandling/conditional serialization (ShouldSerializeX / ShouldSerialize prefix).","Use JsonSerializerSettings.Error to handle and skip the member instead of aborting the whole graph."],"exampleFix":"// before: getter throws when context disposed\npublic class Order { public Customer Customer => _ctx.Customers.Find(Id); }\nJsonConvert.Serialize(order); // throws\n// after: guard + ignore\npublic class Order {\n    public bool ShouldSerializeCustomer() => _ctx != null;\n    public Customer Customer => _ctx?.Customers.Find(Id);\n}","handlingStrategy":"validation","validationCode":"public bool ShouldSerializeX() => _dependency != null && _state.IsValid;","typeGuard":null,"tryCatchPattern":"try {\n    var json = JsonConvert.SerializeObject(obj, settings);\n} catch (JsonSerializationException ex) when (ex.Message.Contains(\"Error getting value\")) {\n    logger.Error(ex, \"Getter failed at {Path}: {Inner}\", ex.Path, ex.InnerException?.Message);\n    throw;\n}","preventionTips":["Make property getters safe: guard against null/disposed dependencies.","Use ShouldSerializeX / ShouldSerialize prefix to skip members whose state isn't ready.","Decorate volatile getters with [JsonIgnore].","Set JsonSerializerSettings.Error to handle and continue."],"tags":["serialization","reflection","dynamic-value-provider","getter"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}