I am using Blazor with Radzen Components. I have a page setup with two tabs. The first tab lists items that can be edited. The second tab references a component used to edit the selected item.
The code under the Edit button:
private TestVS selectedSchedule = new();private async void EditVessel(VesselListItem vessel){ selectedSchedule.VesselLoadId = vessel.VesselLoadId; await selectedSchedule.LoadVessel(); tabsSelectedIndex = 1;}When TestVS is created, the OnitializedAsync() method is executed:
protected override async Task OnInitializedAsync(){ try { if (CurrentUserIsValid) { // Build drop down lists var vList = await vesselData.GetVesselLoadList(); if (vList is not null) { vesselList = vList.ToList(); } agentList = await vesselData.GetVesselAgentList(); destinationList = await vesselData.GetVesselDestinationList(); shipperList = await shipperData.GetList(); customerList = await customerData.GetList(); } else { try { navigation.NavigateTo("/"); } catch { } } } catch (Exception ex) { LogError(ex); }}This simply loads data for dropdowns on the form. The LoadVessel() method simply makes one more call to the DB to retrieve information:
public async Task LoadVessel(){ try { if (VesselLoadId > 0) { vesselLoad = await vesselData.GetVesselLoadById(VesselLoadId); } } catch (Exception ex) { LogError(ex); } // This throws a Null Reference Exception StateHasChanged();}The [...]Data objects are injected and used for database operations. This represents a standard call to get data:
public async Task<VesselLoad?> GetVesselLoadById(int id){ var itemList = await _sql.LoadData<VesselLoad, dynamic>("dbo.spVesselLoad_GetById", new { VesselLoadId = id }); if (itemList is not null) { return itemList.FirstOrDefault<VesselLoad>(); } return null;}I use this approach throughout this application and haven't had any problems until now. I've tried creating another method call Refresh() in TestVS that simply lets me call StateHasChanged() after the LoadVessel method is complete, but it still throws the null reference exception.
I can provide more code snippets if necessary. A much more complicated setup used to work fine for this page, but has now started failing inexplicably. If anyone has a clue, I could sure use one.