0

I'm trying to update an object with another object asynchronously. I'm trying to get the CustomerId value from Statues and then use it to call a specific customer and pass those values into PreviousStatuses. Then update the StatusToAdd with PreviousStatuses. If I pass Statues to StatusToAdd the values update. However, it's the wrong customer id. That's why I'm using PreviousStatuses.

This is the error I get:

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 DSPDRewrite.Pages.Popups.AddStatusComponent.UpdateValues(String id)
   at DSPDRewrite.Pages.Popups.AddStatusComponent.OnInitializedAsync()
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)




 [Parameter]
    public int CustomerId { get; set; }

[CascadingParameter]
public Task<AuthenticationState> AuthState { get; set; }

public Status Statuses;
public Status PreviousStatuses;
//IList<Status> PreviousStatuses;

public Dspd1056Status StatusToAdd = new Dspd1056Status();

public Customer customer;

public int AccountStatusId = 0;



protected override async Task OnInitializedAsync()
{
    Statuses = await dataService.Get1056StatusById(CustomerId);
    //int id = Int32.Parse(Statuses.CustomerId);
    //  Statuses = await dataService.Get1056StatusById(id);
    Console.WriteLine(Statuses.CustomerId);
    await UpdateValues(Statuses.CustomerId);

}
async Task UpdateValues(string id)
{
    PreviousStatuses = await dataService.Get1056StatusById(Int32.Parse((id)));
    StatusToAdd.AccountCurrent = PreviousStatuses.AccountCurrent;
    StatusToAdd.StartDate = PreviousStatuses.StartDate;
    StatusToAdd.EndDate = PreviousStatuses.EndDate;
    StatusToAdd.Units = PreviousStatuses.Units;
    StatusToAdd.Ppc = PreviousStatuses.Ppc;
    StatusToAdd.EndStatus = PreviousStatuses.EndStatus;
    StatusToAdd.ContinuallyFunded = PreviousStatuses.ContinuallyFunded;
    StatusToAdd.AnnualUnits = PreviousStatuses.AnnualUnits;
    StatusToAdd.Elg = PreviousStatuses.Elg;
    StatusToAdd.ReceiptDate = PreviousStatuses.ReceiptDate;
    StatusToAdd.RahTripsFunded = PreviousStatuses.RahTripsFunded;
    StatusToAdd.Rate = PreviousStatuses.Rate;
    StatusToAdd.AccountTotal = PreviousStatuses.AccountTotal;
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
    if (firstRender)
    {
        if (CustomerId != 0)
        {
            customer = await dataService.GetCustomerById((int)CustomerId);
            StateHasChanged();
        }
    }
}
Brsn
  • 11
  • 1
  • With the code as shown, what is the value of `Statuses` on the line `Console.WriteLine(Statuses.CustomerId)`, and the value of `PreviousStatuses` in the line `StatusToAdd.AccountCurrent = PreviousStatuses.AccountCurrent;`. Also have you stepped through `UpdateValues` in debug mode to see which line errors? – MrC aka Shaun Curtis Oct 06 '21 at 15:11
  • I have, the error comes from this line: int id = Int32.Parse(Statuses.CustomerId); Statuses can change depending on which customer I select. – Brsn Oct 06 '21 at 15:37
  • 1
    The error you've shown - `DSPDRewrite.Pages.Popups.AddStatusComponent.UpdateValues(String id)` is not for that error - it's for something in `UpdateValues`. In the line you've shown any error is either because `dataService.Get1056StatusById` returned a null value or `Statuses.CustomerId` is null. You need to step through your code in debug mode to figure out who is the culprit. – MrC aka Shaun Curtis Oct 06 '21 at 15:52
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Dour High Arch Oct 06 '21 at 19:04

1 Answers1

0

A few things to consider:

OnInitializedAsync

Can your .Get1056StatusById() method return null (e.g. if CustomerId is not found)? If so that appears to be the cause of the error: your code calls

await UpdateValues(Statuses.CustomerId);

That suggests Statuses is null. I would add a null check to this code to handle that case. Not sure why the Console.WriteLine didn't throw the null reference first, I assume the sample code didn't have it when the error was thrown.

A few other points: fields and methods in Components should normally be private or protected. If you are going to expose outside the Component they should be properties. Methods are rarely called outside a component. My suggested changes would be:

[Parameter] public int CustomerId { get; set; }

[CascadingParameter] public Task<AuthenticationState> AuthState { get; set; }

Status Statuses;
Status PreviousStatuses;

Dspd1056Status StatusToAdd = new Dspd1056Status();

Customer customer;

int AccountStatusId = 0;

protected override async Task OnInitializedAsync()
{
    Statuses = await dataService.Get1056StatusById(CustomerId);
    if(Statuses != null)
    {
       Console.WriteLine(Statuses.CustomerId);
       await UpdateValues(Statuses.CustomerId);
    }
}
async Task UpdateValues(string id)
{
    PreviousStatuses = await dataService.Get1056StatusById(Int32.Parse((id)));
    // rest...

Your component HTML should obviously check for a null Statuses before it tries to render:

@if(Statuses != null)
{
    <p>Customer is @Statuses.CustomerId</p>
}

This is such a frequently used pattern in Blazor apps I usually use a <NotNull> component: https://gist.github.com/conficient/eab57ade2587104d71ac6f26ddfd4865

Quango
  • 12,338
  • 6
  • 48
  • 83