{"record":{"id":"7efa8a944fb6b7ee","repo":"PrismLibrary/Prism","slug":"region-regioncollection","errorCode":null,"errorMessage":"region","messagePattern":"region","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Maui/Prism.Maui/Navigation/Regions/RegionCollection.cs","lineNumber":50,"sourceCode":"    {\n        get\n        {\n            Xaml.RegionManager.UpdateRegions();\n\n            IRegion region = GetRegionByName(regionName);\n            if (region == null)\n            {\n                throw new KeyNotFoundException(string.Format(CultureInfo.CurrentUICulture, Resources.RegionNotInRegionManagerException, regionName));\n            }\n\n            return region;\n        }\n    }\n\n    public void Add(IRegion region)\n    {\n        if (region == null)\n            throw new ArgumentNullException(nameof(region));\n\n        Xaml.RegionManager.UpdateRegions();\n\n        if (region.Name == null)\n        {\n            throw new InvalidOperationException(Resources.RegionNameCannotBeEmptyException);\n        }\n\n        if (GetRegionByName(region.Name) != null)\n        {\n            throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,\n                                                      Resources.RegionNameExistsException, region.Name));\n        }\n\n        _regions.Add(region);\n        region.RegionManager = regionManager;\n\n        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, region, 0));","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/Navigation/Regions/RegionCollection.cs#L32-L68","documentation":"Fires in the RegionCollection indexer getter when the requested region name has not been registered with the RegionManager. UpdateRegions() runs first so XAML-declared regions are registered; if GetRegionByName still finds nothing, the caller asked for a region that does not exist (wrong name, or region never created/registered).","triggerScenarios":"Calling regionManager.Regions.Add(null) directly, or a factory/GetRegion callback that returned null and its result is passed to Add.","commonSituations":"Custom IRegion creation code where a region resolver/adapter returned null; DI container returning null for a region type; test code constructing region collections.","solutions":["Null-check the region before adding; trace why the producer returned null.","Fix the custom region factory/adapter so it returns a valid IRegion instance.","Verify DI registration for the custom IRegion type actually maps to an implementation."],"exampleFix":"// before\nregionManager.Regions.Add(CreateRegion()); // may be null\n\n// after\nvar region = CreateRegion();\nif (region != null)\n    regionManager.Regions.Add(region);","handlingStrategy":"validation","validationCode":"if (region == null) throw new InvalidOperationException(\"Region factory returned null\");","typeGuard":null,"tryCatchPattern":"try { regionManager.Regions.Add(region); } catch (ArgumentNullException ex) { logger.LogError(ex, \"Attempted to add null region\"); }","preventionTips":["Null-check results of region factories/resolvers before Add","Verify DI registrations for custom IRegion implementations","Never call Regions.Add directly with values of nullable reference types without checks"],"tags":["prism","maui","regions","null-argument"],"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"}