dotnet/maui · error · ArgumentNullException

binding

Error message

binding

What it means

Thrown inside the internal SetBinding flow at the line that stores the binding: 'context.Bindings[specificity] = binding ?? throw new ArgumentNullException(nameof(binding));'. Despite the null-coalescing throw, it fires when a null BindingBase reaches the apply step, i.e. SetBinding was effectively asked to register a null binding. Note the public SetBinding allows a null binding through to compute specificity, so this guard is the actual null-binding enforcement point.

Source

Thrown at src/Controls/src/Core/BindableObject.cs:343

			BindingBase oldBinding = null;
			SetterSpecificity oldSpecificity = default;
			if (context.Bindings.Count > 0)
			{
				var b_p = context.Bindings.GetSpecificityAndValue();
				oldSpecificity = b_p.Key;
				oldBinding = b_p.Value;
			}

			if (oldBinding != null && specificity < oldSpecificity)
			{
				context.Bindings[specificity] = binding;
				return;
			}

			oldBinding?.Unapply();

			context.Bindings[specificity] = binding ?? throw new ArgumentNullException(nameof(binding));

			targetProperty.BindingChanging?.Invoke(this, oldBinding, binding);

			binding.Apply(BindingContext, this, targetProperty, false, specificity);
		}

		/// <summary>
		/// Sets the inherited context to a nested element.
		/// </summary>
		/// <param name="bindable">The object on which to set the inherited binding context.</param>
		/// <param name="value">The inherited context to set.</param>
		/// <remarks>For internal use only. This API can be changed or removed without notice at any time.</remarks>
		[EditorBrowsable(EditorBrowsableState.Never)]
		public static void SetInheritedBindingContext(BindableObject bindable, object value)
		{
			// I wonder if we couldn't treat BindingContext with specificities
			BindablePropertyContext bpContext = bindable.GetContext(BindingContextProperty);
			if (bpContext != null && bpContext.Values.GetSpecificity() >= SetterSpecificity.ManualValueSetter)

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Do not call SetBinding with a null binding — call RemoveBinding instead if you intended to clear it.
  2. Null-check the binding before calling SetBinding: 'if (binding is not null) target.SetBinding(prop, binding);'.
  3. Fix the binding factory so it never returns null.

Example fix

// before
obj.SetBinding(prop, maybeNullBinding);
// after
if (maybeNullBinding is not null)
    obj.SetBinding(prop, maybeNullBinding);
else
    obj.RemoveBinding(prop);
Defensive patterns

Strategy: validation

Validate before calling

if (binding is null) { obj.RemoveBinding(targetProperty); return; }
obj.SetBinding(targetProperty, binding);

Type guard

static bool IsUsableBinding(Microsoft.Maui.Controls.BindingBase b) => b is not null;

Try / catch

try { obj.SetBinding(prop, binding); }
catch (ArgumentNullException ex) when (ex.ParamName == "binding")
{ /* binding was null; clear instead or log */ }

Prevention

When it happens

Trigger: Calling SetBinding(targetProperty, null); a binding factory returning null; a conditional binding expression that produced null and was forwarded without a check.

Common situations: Conditional UI code that builds a binding only sometimes and passes null otherwise; migrating code that used to call SetBinding with a default and now passes null after a refactor.

Related errors


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