{"record":{"id":"7f11fcf04bc50572","repo":"dotnet/maui","slug":"cannot-apply-relative-binding-when-the-target-obje","errorCode":null,"errorMessage":"Cannot apply relative binding when the target object is null.","messagePattern":"Cannot apply relative binding when the target object is null\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/Binding.cs","lineNumber":165,"sourceCode":"\t\t\tobject src = _source;\n\t\t\tvar isApplied = IsApplied;\n\n\t\t\tvar bindingContext = src ?? Context ?? context;\n\t\t\tbase.Apply(bindingContext, bindObj, targetProperty, fromBindingContextChanged, specificity);\n\n\t\t\tif (src != null && isApplied && fromBindingContextChanged)\n\t\t\t\treturn;\n\n\t\t\tif (Source is RelativeBindingSource relativeBindingSource)\n\t\t\t{\n\t\t\t\tvar relativeSourceTarget = RelativeSourceTargetOverride ?? bindObj as Element;\n\t\t\t\tif (relativeSourceTarget is not Element)\n\t\t\t\t{\n\t\t\t\t\tvar message = bindObj is not null\n\t\t\t\t\t\t? $\"Cannot apply relative binding to {bindObj.GetType().FullName} because it is not a superclass of Element.\"\n\t\t\t\t\t\t: \"Cannot apply relative binding when the target object is null.\";\n\n\t\t\t\t\tthrow new InvalidOperationException(message);\n\t\t\t\t}\n\n\t\t\t\tApplyRelativeSourceBinding(relativeBindingSource, relativeSourceTarget, bindObj, targetProperty, specificity);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (_expression == null)\n\t\t\t\t\t_expression = new BindingExpression(this, SelfPath);\n\t\t\t\t_expression.Apply(bindingContext, bindObj, targetProperty, specificity);\n\t\t\t}\n\t\t}\n\n#pragma warning disable RECS0165 // Asynchronous methods should return a Task instead of void\n\t\tasync void ApplyRelativeSourceBinding(RelativeBindingSource relativeSource, Element relativeSourceTarget, BindableObject targetObject, BindableProperty targetProperty, SetterSpecificity specificity)\n#pragma warning restore RECS0165 // Asynchronous methods should return a Task instead of void\n\t\t{\n\t\t\ttry\n\t\t\t{","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/Binding.cs#L147-L183","documentation":"InvalidOperationException from Binding.Apply when Source is a RelativeBindingSource, RelativeSourceTargetOverride is null, and the target bindObj itself is null. The ternary picks the null-target branch of the message because bindObj is null, so the converter cannot even report a type. Relative bindings require a concrete Element anchor, and a null anchor is unrecoverable.","triggerScenarios":"Calling Apply / SetBinding with a relative-source Binding before the target BindableObject is set (the framework passing a null target); setting RelativeSourceTargetOverride to null explicitly while bindObj is null; a binding applied during teardown when the target has been released.","commonSituations":"Framework lifecycle races where Apply is invoked during disposal; custom hosts that call SetBinding with a null target by mistake; unit tests that construct a Binding and call Apply(null, ...).","solutions":["Ensure the target BindableObject is non-null before applying a relative binding.","Set RelativeSourceTargetOverride to a known Element when the natural target may be transient.","Defer binding application until the target element is fully constructed/loaded."],"exampleFix":"// before\nbinding.Apply(context, target: null, bp, false, specificity);\n\n// after\nif (bindObj is null) return; // guard before applying a relative binding\nbinding.Apply(context, bindObj, bp, false, specificity);","handlingStrategy":"validation","validationCode":"if (bindObj is null)\n    return; // do not apply a relative binding without a target Element\nbinding.Apply(context, bindObj, bp, false, specificity);","typeGuard":"static bool HasValidRelativeTarget(Microsoft.Maui.Controls.BindableObject target,\n    Microsoft.Maui.Controls.Element overrideEl) =>\n    (overrideEl ?? target as Microsoft.Maui.Controls.Element) is not null;","tryCatchPattern":null,"preventionTips":["Defer binding application until the target element is constructed.","Set RelativeSourceTargetOverride when the natural target is transient.","Null-check the target before calling SetBinding during teardown."],"tags":["maui","data-binding","relative-binding","nullable","lifecycle"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}