{"record":{"id":"0c56efa57eeb6f14","repo":"PrismLibrary/Prism","slug":"regiontarget-regionadapterbase","errorCode":null,"errorMessage":"regionTarget","messagePattern":"regionTarget","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Maui/Prism.Maui/Navigation/Regions/Adapters/RegionAdapterBase.cs","lineNumber":129,"sourceCode":"\n    /// <summary>\n    /// Template method to adapt the object to an <see cref=\"IRegion\"/>.\n    /// </summary>\n    /// <param name=\"region\">The new region being used.</param>\n    /// <param name=\"regionTarget\">The object to adapt.</param>\n    protected abstract void Adapt(IRegion region, T regionTarget);\n\n    /// <summary>\n    /// Template method to create a new instance of <see cref=\"IRegion\"/>\n    /// that will be used to adapt the object.\n    /// </summary>\n    /// <returns>A new instance of <see cref=\"IRegion\"/>.</returns>\n    protected abstract IRegion CreateRegion(IContainerProvider container);\n\n    private static T GetCastedObject(object regionTarget)\n    {\n        if (regionTarget == null)\n            throw new ArgumentNullException(nameof(regionTarget));\n\n        if (regionTarget is not T castedObject)\n            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.AdapterInvalidTypeException, typeof(T).Name));\n\n        return castedObject;\n    }\n\n    private static void SetObservableRegionOnHostingControl(IRegion region, T regionTarget)\n    {\n        if (regionTarget is VisualElement targetElement)\n        {\n            // Set the region as a dependency property on the control hosting the region\n            // Because we are using an observable region, the hosting control can detect that the\n            // region has actually been created. This is an ideal moment to hook up custom behaviors\n            Xaml.RegionManager.GetObservableRegion(targetElement).Value = region;\n        }\n    }\n}","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/Navigation/Regions/Adapters/RegionAdapterBase.cs#L111-L147","documentation":"RegionAdapterBase<T>.GetCastedObject receives the regionTarget as object and throws ArgumentNullException when it is null, before attempting the cast to the adapter's control type T. It is the first guard invoked from Initialize.","triggerScenarios":"Calling any IRegionAdapter.Initialize(null, \"RegionName\"), or resolving an adapter through RegionAdapterMappings.GetMapping(null). Happens when region discovery hands a null control to the adapter pipeline.","commonSituations":"Programmatically registering regions with an uninitialized control reference; a XAML element failing to instantiate so the adapter target is null; custom adapter code passing an unresolved view.","solutions":["Fix the caller so a valid control instance is passed to Initialize / GetMapping","Check that the view containing the region actually instantiated (no XAML compile/load failure producing null)","If writing custom region setup code, validate the control for null before calling the adapter"],"exampleFix":"// before\nadapter.Initialize(someLayout, \"MyRegion\"); // someLayout is null\n\n// after\nif (someLayout is null)\n    throw new InvalidOperationException(\"Region target control was not resolved\");\nadapter.Initialize(someLayout, \"MyRegion\");","handlingStrategy":"type-guard","validationCode":"if (regionTarget is null) throw new InvalidOperationException(\"Region target control was not resolved before Initialize\");","typeGuard":"bool CanAdapt(object regionTarget) => regionTarget is not null;","tryCatchPattern":"try { adapter.Initialize(target, name); }\ncatch (ArgumentNullException) { /* target control was null; check XAML instantiation */ }","preventionTips":["Verify region host controls instantiate correctly (no XAML load failures)","Null-check controls before programmatic region registration","Avoid registering regions in constructors where controls may not be built yet"],"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"}