LykosAI/StabilityMatrix · error · NullReferenceException

DockControl not found

Error message

DockControl not found

What it means

DockUserControlBase.OnApplyTemplate resolves the template child named 'Dock' as a DockControl via FindControl. If the applied control template lacks a DockControl named 'Dock', a NullReferenceException('DockControl not found') is thrown, aborting template application and breaking the control's dock layout setup.

Solutions

  1. Add <controls:DockControl x:Name="Dock"/> to the control's ControlTemplate
  2. Ensure the control's default style/template is actually applied (Themes included, StyleKey matches)
  3. Keep the template part name 'Dock' unchanged and use TemplatePart attributes for documentation
  4. Check that OnApplyTemplate is firing (template attached) rather than FindControl returning null from a different namescope

Example fix

// before (template missing part)
<ControlTemplate><ContentPresenter/></ControlTemplate>
// after
<ControlTemplate>
  <dock:DockControl x:Name="Dock"/>
</ControlTemplate>
Defensive patterns

Strategy: validation

Validate before calling

var dock = this.FindControl<DockControl>("Dock");
if (dock is null) { Log.Error("Template missing Dock part"); return; }

Type guard

bool HasDockPart(TemplatedControl c) => c.Template != null && c.FindControl<DockControl>("Dock") != null;

Try / catch

try { baseDock = this.FindControl<DockControl>("Dock")!; }
catch (NullReferenceException ex) { Log.Error(ex, "Dock template part missing"); }

Prevention

When it happens

Trigger: The control's XAML ControlTemplate does not include <DockControl Name="Dock"/>, the template is not applied (control used without its default style), or FindControl returns null because the part was renamed or nested in another name scope.

Common situations: Custom restyle of the control that drops the required Dock part; using the control with a theme that doesn't ship its template; renaming the x:Name in the template during refactoring.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/4be9490448a8e91b. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Avalonia/Controls/Dock/DockUserControlBase.cs:37

/// Base for Dock controls
/// Expects a <see cref="DockControl"/> named "Dock" in the XAML
/// </summary>
public abstract class DockUserControlBase : DropTargetUserControlBase
{
    private DockControl? baseDock;
    private readonly DockSerializer dockSerializer = new(typeof(AvaloniaList<>));
    private readonly DockState dockState = new();
    private readonly DockState initialDockState = new();
    private IDock? initialLayout;

    /// <inheritdoc />
    protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
    {
        base.OnApplyTemplate(e);

        baseDock =
            this.FindControl<DockControl>("Dock")
            ?? throw new NullReferenceException("DockControl not found");

        if (baseDock.Layout is { } layout)
        {
            dockState.Save(layout);
            initialDockState.Save(layout);
            initialLayout = layout;
            // Dispatcher.UIThread.Post(() => dockState.Save(layout), DispatcherPriority.Background);
        }
    }

    /// <inheritdoc />
    protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
    {
        base.OnAttachedToVisualTree(e);

        // Attach handlers for view state saving and loading
        if (DataContext is InferenceTabViewModelBase vm)
        {

View on GitHub (pinned to af93d6ef57)