PrismLibrary/Prism · error · NullReferenceException

The resolved type for

Error message

The resolved type for {segmentName} was null. You may be attempting to navigate to a Non-Page type

What it means

During CreatePage, Prism resolved a view for the segment via Registry.CreateView but the result could not be cast/returned as a Page (null). This indicates the registration mapped the name to something that is not a Xamarin/MAUI Page.

Solutions

  1. Register the segment name against a Page type: containerRegistry.RegisterForNavigation<ViewB, ViewBViewModel>()
  2. Verify the registered type derives from Page (ContentPage, TabbedPage, NavigationPage...)
  3. If using a custom view-creation factory, ensure it never returns null and throws a descriptive exception instead

Example fix

// before
containerRegistry.Register<ViewBViewModel>(); // name 'ViewB' resolves to a VM, not a Page
// after
containerRegistry.RegisterForNavigation<ViewB, ViewBViewModel>();
Defensive patterns

Strategy: type-guard

Validate before calling

var known = containerRegistry.IsRegistered<Page>(segmentName); // verify before navigating

Type guard

static bool IsNavigablePage(object? v) => v is Page;

Try / catch

try { await _navigationService.NavigateAsync(segment); }
catch (NullReferenceException nre) when (nre.Message.Contains("Non-Page type")) { /* fix registration */ }

Prevention

When it happens

Trigger: Navigating to a segment name whose container registration resolves a non-Page (service, view model, ContentView) or whose factory returns null.

Common situations: Registering a ViewModel by the same name used for navigation; registering a ContentView/Control instead of a ContentPage; a custom registry/factory returning null for unconfigured names.

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 PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/4020f7004bf76d02. Report an issue: GitHub.

Appendix: source

Thrown at src/Maui/Prism.Maui/Navigation/PageNavigationService.cs:937

            {
                MvvmHelpers.OnNavigatedFrom(navigationPage.CurrentPage, parameters);
            }
            else if (tabbedPage.BindingContext != tabbedPage.CurrentPage.BindingContext)
            {
                MvvmHelpers.OnNavigatedFrom(tabbedPage.CurrentPage, parameters);
            }
        }
    }

    protected virtual Page CreatePage(string segmentName)
    {
        try
        {
            var scope = _container.CreateScope();
            var page = (Page)Registry.CreateView(scope, segmentName);

            if (page is null)
                throw new NullReferenceException($"The resolved type for {segmentName} was null. You may be attempting to navigate to a Non-Page type");

            return page;
        }
        catch(NavigationException)
        {
            throw;
        }
        catch(KeyNotFoundException knfe)
        {
            throw new NavigationException(NavigationException.NoPageIsRegistered, segmentName, knfe);
        }
        catch(ViewModelCreationException vmce)
        {
            throw new NavigationException(NavigationException.ErrorCreatingViewModel, segmentName, _pageAccessor.Page, vmce);
        }
        //catch(ViewCreationException viewCreationException)
        //{
        //    if(!string.IsNullOrEmpty(viewCreationException.InnerException?.Message) && viewCreationException.InnerException.Message.Contains("Maui"))

View on GitHub (pinned to 358118cd64)