PrismLibrary/Prism · error · ArgumentNullException

Object reference not set (ArgumentNullException for…

Error message

Object reference not set (ArgumentNullException for 'region')

What it means

A sentinel ArgumentNullException guard in RegionCollection.Add: the caller passed a null IRegion reference. Adding a region requires a valid instance with a non-null Name, so null is rejected immediately before any registration occurs.

Solutions

  1. Fix the upstream code that produces the null region (failed DI resolution, unassigned variable).
  2. Null-check the region before calling Add and log/handle the failure.
  3. Use ArgumentNullException.ThrowIfNull(region) at your own call site to fail fast with a clearer stack.

Example fix

// before
regionManager.Regions.Add("MainRegion", ResolveRegion()); // ResolveRegion() returned null
// after
var region = ResolveRegion() ?? throw new InvalidOperationException("Region could not be resolved");
regionManager.Regions.Add("MainRegion", region);
Defensive patterns

Strategy: validation

Validate before calling

if (region is null) throw new InvalidOperationException("Cannot add a null region");
regionManager.Regions.Add(name, region);

Type guard

bool CanAddRegion(IRegion r) => r is not null;

Try / catch

try { regionManager.Regions.Add(name, region); }
catch (ArgumentNullException) { Console.WriteLine("Region was null — check DI resolution"); }

Prevention

When it happens

Trigger: Calling Regions.Add(name, null) — typically when a region factory, resolver, or property returns null and its result is passed directly to Add.

Common situations: Dependency injection failing to construct the region so the factory returns null; a conditional expression evaluating to null before Add; copy-pasted registration code where the region variable was never assigned.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/18c26921e43e4ad4. Report an issue: GitHub.

Appendix: source

Thrown at src/Maui/Prism.Maui/Navigation/Regions/RegionCollection.cs:107

    public bool ContainsRegionWithName(string regionName)
    {
        Xaml.RegionManager.UpdateRegions();

        return GetRegionByName(regionName) != null;
    }

    /// <summary>
    /// Adds a region to the <see cref="RegionManager"/> with the name received as argument.
    /// </summary>
    /// <param name="regionName">The name to be given to the region.</param>
    /// <param name="region">The region to be added to the <see cref="RegionManager"/>.</param>
    /// <exception cref="ArgumentNullException">Thrown if <paramref name="region"/> is <see langword="null"/>.</exception>
    /// <exception cref="ArgumentException">Thrown if <paramref name="regionName"/> and <paramref name="region"/>'s name do not match and the <paramref name="region"/> <see cref="IRegion.Name"/> is not <see langword="null"/>.</exception>
    public void Add(string regionName, IRegion region)
    {
        if (region == null)
            throw new ArgumentNullException(nameof(region));

        if (region.Name != null && region.Name != regionName)
            throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.RegionManagerWithDifferentNameException, region.Name, regionName), nameof(regionName));

        if (region.Name == null)
            region.Name = regionName;

        Add(region);
    }

    private IRegion GetRegionByName(string regionName) =>
        _regions.FirstOrDefault(r => r.Name == regionName);

    private void OnCollectionChanged(NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs) =>
        CollectionChanged?.Invoke(this, notifyCollectionChangedEventArgs);
}

View on GitHub (pinned to 358118cd64)