AvaloniaUI/Avalonia · error · InvalidOperationException

Either 'e' or 'container' must be non-null.

Error message

Either 'e' or 'container' must be non-null.

What it means

TabNavigation.GetPrevTab derives the container (group parent) from the element `e` when a container is not supplied. If both `e` and `container` are null there is no way to find the tab group, so it throws InvalidOperationException to fail fast on an unresolvable call.

Source

Thrown at src/Avalonia.Base/Input/Navigation/TabNavigation.cs:92

            }

            return null;
        }

        public static IInputElement? GetNextTabOutside(ICustomKeyboardNavigation e)
        {
            if (e is IInputElement container && GetLastInTree(container) is { } last)
            {
                return GetNextTab(last, false);
            }

            return null;
        }

        public static IInputElement? GetPrevTab(IInputElement? e, IInputElement? container, bool goDownOnly)
        {
            container ??=
                GetGroupParent(e ?? throw new InvalidOperationException("Either 'e' or 'container' must be non-null."));

            KeyboardNavigationMode tabbingType = GetKeyNavigationMode(container);

            if (e == null)
            {
                // Using ActiveElement if set
                var activeElement = GetActiveElement(container);
                if (activeElement != null)
                    return GetPrevTab(null, activeElement, true);
                else
                {
                    // If we Shift+Tab on a container with KeyboardNavigationMode=Once, and ActiveElement is null
                    // then we want to go to the first item (not last) within the container
                    if (tabbingType == KeyboardNavigationMode.Once)
                    {
                        var firstTabElement = GetNextTabInGroup(null, container, tabbingType);
                        if (firstTabElement == null)
                        {

View on GitHub (pinned to 11c5427268)

Solutions

  1. Provide at least one non-null argument: the focused element or the tab container.
  2. Guard before calling: `if (e is null && container is null) return null;`.
  3. Ensure the element is in the logical tree so a container can be derived.

Example fix

// before:
var next = TabNavigation.GetPrevTab(null, null, false); // throws

// after:
if (e is null && container is null) return null;
var next = TabNavigation.GetPrevTab(e, container, false);
Defensive patterns

Strategy: validation

Validate before calling

if (e is null && container is null) return null;
return TabNavigation.GetPrevTab(e, container, goDownOnly);

Type guard

static bool HasTarget(IInputElement? e, IInputElement? c) => e is not null || c is not null;

Prevention

When it happens

Trigger: Calling `TabNavigation.GetPrevTab(null, null, goDownOnly)` — no element and no container provided.

Common situations: Passing two nulls by accident from nullable variables. A caller computing `e` and `container` that both resolve to null (e.g. focus not set, parent not found).

Related errors


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