'Unable to get response in the variable in Blazor Web App

I am getting the response in the network tab of developer tools, so that means the call is working fine but my variable for some reason is null in the debugger. I get the following error in the console. "Unhandled exception rendering component: Object reference not set to an instance of an object." Following is the code.

    protected override async Task OnInitializedAsync()
    { 
       Http.DefaultRequestHeaders.Add("some-header-here", "some-value-here");
     // var resp = await Http.GetFromJsonAsync<TeacherLocations>("api-url-here", new JsonSerializerOptions{ PropertyNameCaseInsensitive = true });
       var resp = await Http.GetFromJsonAsync<TeacherLocations>("api-url-here");
       var allLocations = resp.TeacherLocationsList.ToArray();
    }

The model TeachersLocations.

    using Newtonsoft.Json;
    
    namespace Teachers.Shared.Models
    {
        public class TeacherLocations
        {
            [JsonProperty("teacher-locations")]
            public List<City> TeacherLocationsList { get; set; }
        }
    }

City model

namespace Teachers.Shared.Models 
{
    public class City
    {
      public int? _id { get; set; }
      public string? region { get; set; }
      public string? name { get; set; }
    }
}

What can be the problem? I put the breakpoint at resp and it always shows me null.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source