dotnet/maui · error · ArgumentNullException
propertyKey
Error message
propertyKey
What it means
Thrown by the public BindableObject.ClearValue(BindablePropertyKey) overload when propertyKey is null. The key-based overload exists for read-only bindable properties and dereferences propertyKey.BindableProperty, so a null key is rejected immediately.
Source
Thrown at src/Controls/src/Core/BindableObject.cs:120
if (property.IsReadOnly)
{
Application.Current?.FindMauiContext()?.CreateLogger<BindableObject>()?.LogWarning($"Cannot set the BindableProperty \"{property.PropertyName}\" because it is readonly.");
return;
}
ClearValueCore(property, specificity);
}
/// <summary>
/// Clears any value that is previously set for a bindable property, identified by its key.
/// </summary>
/// <param name="propertyKey">The key that identifies the bindable property to clear the value for.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="propertyKey"/> is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">Thrown when <paramref name="propertyKey"/> is a read-only property.</exception>
public void ClearValue(BindablePropertyKey propertyKey)
{
if (propertyKey == null)
throw new ArgumentNullException(nameof(propertyKey));
ClearValueCore(propertyKey.BindableProperty, SetterSpecificity.ManualValueSetter);
}
void ClearValueCore(BindableProperty property, SetterSpecificity specificity)
{
BindablePropertyContext bpcontext = GetContext(property);
if (bpcontext == null)
return;
var original = bpcontext.Values.GetSpecificityAndValue();
if (original.Key == SetterSpecificity.FromHandler)
{
bpcontext.Values.Remove(SetterSpecificity.FromHandler);
}
var newValue = bpcontext.Values.GetClearedValue(specificity);View on GitHub (pinned to f377ff1c5e)
Solutions
- Pass the actual BindablePropertyKey returned when the read-only BindableProperty was created (the static readonly field).
- Null-check the key before clearing: if (key is not null) obj.ClearValue(key);.
- If you only have the BindableProperty, use the property-based ClearValue overload instead.
Example fix
// before obj.ClearValue((BindablePropertyKey)null); // after obj.ClearValue(MyControl.ReadOnlyPropertyKey);
Defensive patterns
Strategy: validation
Validate before calling
static void SafeClearKey(BindableObject obj, BindablePropertyKey? key)
{
if (key is null) return;
obj.ClearValue(key);
} Type guard
static bool IsValidKey(BindablePropertyKey? k) => k is not null;
Prevention
- Use the static readonly BindablePropertyKey field created alongside the read-only property.
- Null-check the key before clearing when the source may be absent.
- If you only hold the BindableProperty, use the property-based ClearValue overload instead.
When it happens
Trigger: Calling ClearValue((BindablePropertyKey)null) or passing a key variable that resolved to null, reaching 'throw new ArgumentNullException(nameof(propertyKey))'.
Common situations: Clearing a read-only property whose key field was never assigned; holding a BindablePropertyKey in a dictionary that returns null; refactor that renamed the key field and left the old reference null.
Related errors
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/451be564d45e2c10.
Report an issue: GitHub.