AvaloniaUI/Avalonia · error · InvalidOperationException

Not selector must have a selector argument.

Error message

Not selector must have a selector argument.

What it means

Thrown by the NotSelector constructor when its `argument` parameter is null. The `:not()` selector requires a valid inner selector to invert. The public `Selectors.Not()` overloads pass through the argument, so a null arises only when a Func<Selector?,Selector> returns null or a null Selector is passed directly.

Source

Thrown at src/Avalonia.Base/Styling/NotSelector.cs:25

{
    /// <summary>
    /// The `:not()` style selector.
    /// </summary>
    internal class NotSelector : Selector
    {
        private readonly Selector? _previous;
        private readonly Selector _argument;
        private string? _selectorString;

        /// <summary>
        /// Initializes a new instance of the <see cref="NotSelector"/> class.
        /// </summary>
        /// <param name="previous">The previous selector.</param>
        /// <param name="argument">The selector to be not-ed.</param>
        public NotSelector(Selector? previous, Selector argument)
        {
            _previous = previous;
            _argument = argument ?? throw new InvalidOperationException("Not selector must have a selector argument.");
        }

        /// <inheritdoc/>
        internal override bool InTemplate => _argument.InTemplate;

        /// <inheritdoc/>
        internal override bool IsCombinator => false;

        /// <inheritdoc/>
        internal override Type? TargetType => _previous?.TargetType;

        /// <inheritdoc/>
        public override string ToString(Style? owner)
        {
            if (_selectorString == null)
            {
                _selectorString = $"{_previous?.ToString(owner, true)}:not({_argument})";
            }

View on GitHub (pinned to 11c5427268)

Solutions

  1. Ensure the argument to Not() is a non-null selector, e.g. `s.Not(x => x.Class("disabled"))`.
  2. If building from data, guard against null before calling Not().
  3. In AXAML, make sure `:not(...)` contains a real selector expression.

Example fix

// before
selector = selector.Not(_ => GetSelectorMaybeNull()); // may return null

// after
var inner = GetSelectorMaybeNull();
if (inner is not null)
    selector = selector.Not(inner);
Defensive patterns

Strategy: validation

Validate before calling

Selector? inner = factory(null);
if (inner is null) throw new InvalidOperationException("Not() requires a non-null inner selector.");
selector = selector.Not(inner);

Type guard

static bool IsValidNotArgument(Selector? arg) => arg is not null;

Prevention

When it happens

Trigger: Calling `selector.Not(s => null)` where the lambda returns null, or `new NotSelector(prev, null)` / `Selectors.Not(prev, (Selector)null)`. Also possible via a malformed AXAML `:not()` with empty content.

Common situations: Writing a Not() factory lambda that conditionally returns null; building selectors reflectively or from data where the inner selector is missing; AXAML `Selector="...:not()"` with nothing inside the parentheses.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/0ff1e01b86ae714c. Report an issue: GitHub.