{"record":{"id":"18c26921e43e4ad4","repo":"PrismLibrary/Prism","slug":"object-reference-not-set-argumentnullexception-for-region","errorCode":null,"errorMessage":"Object reference not set (ArgumentNullException for 'region')","messagePattern":"Object reference not set \\(ArgumentNullException for 'region'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Maui/Prism.Maui/Navigation/Regions/RegionCollection.cs","lineNumber":107,"sourceCode":"\n    public bool ContainsRegionWithName(string regionName)\n    {\n        Xaml.RegionManager.UpdateRegions();\n\n        return GetRegionByName(regionName) != null;\n    }\n\n    /// <summary>\n    /// Adds a region to the <see cref=\"RegionManager\"/> with the name received as argument.\n    /// </summary>\n    /// <param name=\"regionName\">The name to be given to the region.</param>\n    /// <param name=\"region\">The region to be added to the <see cref=\"RegionManager\"/>.</param>\n    /// <exception cref=\"ArgumentNullException\">Thrown if <paramref name=\"region\"/> is <see langword=\"null\"/>.</exception>\n    /// <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>\n    public void Add(string regionName, IRegion region)\n    {\n        if (region == null)\n            throw new ArgumentNullException(nameof(region));\n\n        if (region.Name != null && region.Name != regionName)\n            throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.RegionManagerWithDifferentNameException, region.Name, regionName), nameof(regionName));\n\n        if (region.Name == null)\n            region.Name = regionName;\n\n        Add(region);\n    }\n\n    private IRegion GetRegionByName(string regionName) =>\n        _regions.FirstOrDefault(r => r.Name == regionName);\n\n    private void OnCollectionChanged(NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs) =>\n        CollectionChanged?.Invoke(this, notifyCollectionChangedEventArgs);\n}\n","sourceCodeStart":89,"sourceCodeEnd":124,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/Navigation/Regions/RegionCollection.cs#L89-L124","documentation":"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.","triggerScenarios":"Calling Regions.Add(name, null) — typically when a region factory, resolver, or property returns null and its result is passed directly to Add.","commonSituations":"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.","solutions":["Fix the upstream code that produces the null region (failed DI resolution, unassigned variable).","Null-check the region before calling Add and log/handle the failure.","Use ArgumentNullException.ThrowIfNull(region) at your own call site to fail fast with a clearer stack."],"exampleFix":"// before\nregionManager.Regions.Add(\"MainRegion\", ResolveRegion()); // ResolveRegion() returned null\n// after\nvar region = ResolveRegion() ?? throw new InvalidOperationException(\"Region could not be resolved\");\nregionManager.Regions.Add(\"MainRegion\", region);","handlingStrategy":"validation","validationCode":"if (region is null) throw new InvalidOperationException(\"Cannot add a null region\");\nregionManager.Regions.Add(name, region);","typeGuard":"bool CanAddRegion(IRegion r) => r is not null;","tryCatchPattern":"try { regionManager.Regions.Add(name, region); }\ncatch (ArgumentNullException) { Console.WriteLine(\"Region was null — check DI resolution\"); }","preventionTips":["Never pass factory results to Add without a null check","Fail fast at your own boundary with ArgumentNullException.ThrowIfNull","Verify DI registrations that produce regions"],"tags":["prism","regions","null-argument","argumentnullexception"],"backgroundTag":"null-argument","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}