dotnet/maui · error · ArgumentNullException

self

Error message

self

What it means

Thrown by the BindableObjectExtensions.SetBinding(this BindableObject, BindableProperty, string, ...) extension method when self (the BindableObject) is null. This string-path overload builds a Binding from the path and forwards to instance.SetBinding; the guard prevents a NullReferenceException when constructing/invoking the binding.

Source

Thrown at src/Controls/src/Core/BindableObjectExtensions.cs:65

				setChildBindingContext(bo, bc);
			}
		}

		/// <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; }

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Ensure the BindableObject target is non-null before calling the extension (verify x:Name and initialization order).
  2. Null-check the target: 'if (view is not null) view.SetBinding(prop, path);'.
  3. Confirm FindByName/GetTemplateChild returns the expected control.

Example fix

// before
nameField.SetBinding(Entry.TextProperty, nameof(vm.Name));
// after
if (nameField is not null)
    nameField.SetBinding(Entry.TextProperty, nameof(vm.Name));
Defensive patterns

Strategy: validation

Validate before calling

if (self is null) throw new InvalidOperationException("target BindableObject is null");
self.SetBinding(targetProperty, path);

Type guard

static bool IsUsableTarget(Microsoft.Maui.Controls.BindableObject o) => o is not null;

Prevention

When it happens

Trigger: Calling SetBinding as an extension on a null BindableObject, e.g. '((Entry)null).SetBinding(Entry.TextProperty, "Foo");'; a view that was not yet created; a cell/control returned null from FindByName/GetTemplateChild.

Common situations: XAML code-behind referencing a control before InitializeComponent or with a wrong x:Name; data-template child lookups returning null; conditional UI where the target was conditionally instantiated.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/1f7848a5a2f1e466. Report an issue: GitHub.