dotnet/maui · error · ArgumentNullException
targetProperty
Error message
targetProperty
What it means
Thrown by BindableObjectExtensions.SetBinding(this BindableObject, BindableProperty, string, ...) when targetProperty is null. This string-path extension builds a Binding and forwards to instance.SetBinding; the null guard prevents constructing a binding against an undefined target and matches the guard in the underlying SetBinding.
Source
Thrown at src/Controls/src/Core/BindableObjectExtensions.cs:67
}
}
/// <summary>Creates and applies a binding to a property.</summary>
/// <param name="self">The <see cref="Microsoft.Maui.Controls.BindableObject"/>.</param>
/// <param name="targetProperty">The BindableProperty on which to set a binding.</param>
/// <param name="path">A <see cref="System.String"/> indicating the property path to bind to.</param>
/// <param name="mode">The <see cref="Microsoft.Maui.Controls.BindingMode"/> for the binding. This parameter is optional. Default is <see cref="Microsoft.Maui.Controls.BindingMode.Default"/>.</param>
/// <param name="converter">An <see cref="Microsoft.Maui.Controls.IValueConverter"/> for the binding. This parameter is optional. Default is <see langword="null"/>.</param>
/// <param name="stringFormat">A string used as stringFormat for the binding. This parameter is optional. Default is <see langword="null"/>.</param>
/// <remarks>The following example shows how to use the extension method to set a binding.</remarks>
[RequiresUnreferencedCode(TrimmerConstants.StringPathBindingWarning, Url = TrimmerConstants.ExpressionBasedBindingsDocsUrl)]
public static void SetBinding(this BindableObject self, BindableProperty targetProperty, string path, BindingMode mode = BindingMode.Default, IValueConverter converter = null,
string stringFormat = null)
{
if (self == null)
throw new ArgumentNullException(nameof(self));
if (targetProperty == null)
throw new ArgumentNullException(nameof(targetProperty));
var binding = new Binding(path, mode, converter, stringFormat: stringFormat);
self.SetBinding(targetProperty, binding);
}
#nullable enable
/// <summary>
/// Creates a binding between a property on the source object and a property on the target object.
/// </summary>
/// <remarks>
/// <para>The following example illustrates the setting of a binding using the extension method.</para>
/// <example>
/// <code lang="csharp lang-csharp"><![CDATA[
/// public class PersonViewModel
/// {
/// public string Name { get; set; }
/// public Address? Address { get; set; }
/// // ...View on GitHub (pinned to f377ff1c5e)
Solutions
- Pass the correct static BindableProperty field for the target type.
- Null-check targetProperty before calling when it is resolved dynamically.
- Verify the BindableProperty field exists on the object type you are extending.
Example fix
// before
view.SetBinding(maybeNullProp, "Foo");
// after
if (maybeNullProp is not null)
view.SetBinding(maybeNullProp, "Foo"); Defensive patterns
Strategy: validation
Validate before calling
if (targetProperty is null) throw new InvalidOperationException("targetProperty not resolved");
self.SetBinding(targetProperty, path); Type guard
static bool CanBindStringPath(Microsoft.Maui.Controls.BindableObject o, Microsoft.Maui.Controls.BindableProperty p)
=> o is not null && p is not null; Prevention
- Reference the BindableProperty field with nameof to catch renames at compile time.
- Confirm the field is declared on the type you are binding on.
When it happens
Trigger: Calling the string-path SetBinding with a null BindableProperty field; referencing a property field that was never initialized or was renamed; a property lookup helper returning null.
Common situations: Refactor that renamed a BindableProperty field; binding to a property declared on a different type; generic helpers resolving properties by name.
Related errors
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/fe50abe9cb386e97.
Report an issue: GitHub.