|
| 1 | +--- |
| 2 | +title: DialogFactory is Null |
| 3 | +description: How to troubleshoot, fix and resolve a System.NullReferenceException Object reference not set to an instance of an object, which occurs when the TelerikRootComponent or the Telerik Blazor DialogFactory for predefined dialogs are not setup correctly. |
| 4 | +type: troubleshooting |
| 5 | +page_title: Telerik Blazor DialogFactory is Null |
| 6 | +slug: dialog-kb-dialogfactory-null |
| 7 | +tags: telerik, blazor, dialog, dialogfactory |
| 8 | +ticketid: 1674088, 1673922, 1661511 |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td> |
| 19 | + Dialog for Blazor, <br /> |
| 20 | + RootComponent for Blazor |
| 21 | + </td> |
| 22 | + </tr> |
| 23 | + </tbody> |
| 24 | +</table> |
| 25 | + |
| 26 | +## Description |
| 27 | + |
| 28 | +The following exception may occur when calling the `AlertAsync()`, `ConfirmAsync()`, or `PromptAsync()` methods of the [Telerik Blazor `DialogFactory`]({%slug dialog-predefined%}): |
| 29 | + |
| 30 | +`System.NullReferenceException: Object reference not set to an instance of an object` |
| 31 | + |
| 32 | +The following exception may occur on initial page load: |
| 33 | + |
| 34 | +`InvalidOperationException: Cannot provide a value for property '...' on type '...'. There is no registered service of type 'Telerik.Blazor.DialogFactory'` |
| 35 | + |
| 36 | +## Cause |
| 37 | + |
| 38 | +The null reference exception occurs [if the `DialogFactory` `CascadingParameter` was not populated by the `TelerikRootComponent`]({%slug dialog-predefined%}#telerikrootcomponent-dependency). This in turn indicates one of the following: |
| 39 | + |
| 40 | +* The [`TelerikRootComponent`]({%slug rootcomponent-overview%}) is missing in the Blazor app. |
| 41 | +* The root component is present, but it's in the same `.razor` file as the `DialogFactory` cascading parameter. |
| 42 | +* The Blazor app is using <a href="https://learn.microsoft.com/en-us/aspnet/core/blazor/components/render-modes?view=aspnetcore-9.0#apply-a-render-mode-to-a-component-definition" target="_blank">**Per Page / Component** Interactity Location</a> and the root component is present, but it's in a static `.razor` component or layout. |
| 43 | + |
| 44 | +The invalid operation exception occurs if the app tries to inject the `DialogFactory` as a dependency injection (service), which is not the intended way to use the predefined Telerik dialogs. |
| 45 | + |
| 46 | +## Solution |
| 47 | + |
| 48 | +The following technical requirements ensure correct `DialogFactory` setup: |
| 49 | + |
| 50 | +* [Place a `TelerikRootComponent` in a layout `.razor` file]({%slug rootcomponent-overview%}#using-telerikrootcomponent) in the app when using the application types below. If the `DialogFactory` must be avalable in the layout file itself, then [place the `TelerikRootComponent` in a parent layout file]({%slug getting-started/what-you-need%}#optimal-telerikrootcomponent-usage). |
| 51 | + * [Blazor Web App]({%slug getting-started/web-app%}) with <a href="https://learn.microsoft.com/en-us/aspnet/core/blazor/components/render-modes?view=aspnetcore-9.0#apply-a-render-mode-to-the-entire-app" target="_blank">**Global** Interactivity Location</a>. |
| 52 | + * [WebAssembly standalone app]({%slug getting-started/client-side%}) |
| 53 | + * [Blazor Hybrid app]({%slug getting-started/hybrid-blazor%}) |
| 54 | +* When using a Blazor Web App with <a href="https://learn.microsoft.com/en-us/aspnet/core/blazor/components/render-modes?view=aspnetcore-9.0#apply-a-render-mode-to-a-component-definition" target="_blank">**Per Page / Component** Interactity Location</a>, [place the `TelerikRootComponent` inside an interactive Razor component]({%slug rootcomponent-percomponent%}), which is a parent of the component that consumes the `DialogFactory`. |
| 55 | +* Always [consume the `DialogFactory` as a `[CascadingParameter]`]({%slug dialog-predefined%}), and not as a dependency injection with `@inject`. |
| 56 | + |
| 57 | +## See Also |
| 58 | + |
| 59 | +* [Predefined Blazor Dialogs]({%slug dialog-predefined%}) |
| 60 | +* [TelerikRootComponent Overview]({%slug rootcomponent-overview%}) |
0 commit comments