dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.34k stars 9.98k forks source link

Blazor Client-side error: crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] #29718

Closed MarkitoAlmeida closed 3 years ago

MarkitoAlmeida commented 3 years ago

Hi.

I am working with an APS.NET Blazor client-side application and I am having an issue that I can't understand why is happening.

This is the message I get when the error occurs:

blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Object reference not set to an instance of an object. System.NullReferenceException: Object reference not set to an instance of an object. at Pldwar.Client.Pages.OnLineOrderHierarchyOnDemand+<>cDisplayClass0_0.b6 (Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder builder5) [0x00059] in :0 at Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.AddContent (System.Int32 sequence, Microsoft.AspNetCore.Components.RenderFragment fragment) <0x2f2eb90 + 0x0001e> in :0 at Radzen.Blazor.RadzenTabs.b__0_0 (Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder builder2) <0x35bcd78 + 0x0027a> in :0 at Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.AddContent (System.Int32 sequence, Microsoft.AspNetCore.Components.RenderFragment fragment) <0x2f2eb90 + 0x0001e> in :0 at Microsoft.AspNetCore.Components.CascadingValue`1[TValue].Render (Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder builder) <0x35bca30 + 0x00012> in :0 at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch (Microsoft.AspNetCore.Components.Rendering.RenderBatchBuilder batchBuilder, Microsoft.AspNetCore.Components.RenderFragment renderFragment) <0x2d84bc0 + 0x00062> in :0 at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderInExistingBatch (Microsoft.AspNetCore.Components.Rendering.RenderQueueEntry renderQueueEntry) <0x2d845c0 + 0x0004c> in :0 at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue () <0x2d83af8 + 0x00098> in :0

I have an API that I send a request, like:

public async Task RowExpand(Order order) { if (order.OrderEspecifications == null) { HttpClient httpClient = new HttpClient(); order.OrderEspecifications= await httpClient.GetFromJsonAsync<OrderEspecification>("https://localhost:44364/Order/OrderEspecification/" + order.OrderId); } }

The nested JSON I must recieve is:

`{ "idOrder" : 101010. "articles" : [ { "idArticle" : 202020, "articleName" : "ArticleOne", ...

}, ... ] }`

But everytime I send the request to get this JSON I get the error I mentioned. Don't even finish the debbug. Need help to understand what is this happening.

javiercn commented 3 years ago

@MarkitoAlmeida thanks for contacting us.

The error that you are seeing means you have a null reference exception in the code that renders your component markup. Make sure that the data in your page is initialized to a non-null value or check for null when you are rendering the view.

In your case it is likely caused by the render that happens while the request for data is being processed. Try set an empty array to order.OrderEspecifications and that likely will solve your problem.

Checkout here for more details on how the Blazor rendering process works.

MarkitoAlmeida commented 3 years ago

Thank you, @javiercn for your explanation.

Worked fine.

Best regards, Marcos