1

I am injecting my service in view to populate dropdown in razor like this

@inject Web.Services.MyService  Options

Now I am trying to populate my dropdown like this

<select asp-for="CityID" asp-items="await Options.GetCities();" class="form-control form-control-sm">
            <option>--Select--</option>
</select>

Getcities method is below

public async Task<IEnumerable<SelectListItem>> GetCities()

        {   

            var cities = await _cityrepository.ListAllAsync();

            var items = new List<SelectListItem>
            {
                new SelectListItem() { Value = null, Text = "All", Selected = true }
            };
            foreach (SetupCities city in cities)
            {
                items.Add(new SelectListItem() { Value = city.CityId.ToString(), Text = city.Name});
            }

            return items;
        }

I am getting following error

NullReferenceException: Object reference not set to an instance of an object.
EvolvedX.Web.Services.VMWHService.GetCities() in VMWHService.cs
+
            var cities = await _cityrepository.ListAllAsync();
AspNetCore.Views_Warehouse_CreateWarehouse.ExecuteAsync() in CreateWarehouse.cshtml
+
        <select asp-for="CityID" asp-items="await Options.GetCities();" class="form-control form-control-sm">

I can see as the first await hit getcities the function exit. what is the proper way to handle such scenario M function is exiting before completing the method when it found await that is causing my selectlistitem to be null. I want to know how i modify my code so that selectlistitem get filled null exception will be solved

Zia
  • 473
  • 1
  • 4
  • 17
  • Possible duplicate of [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) – Sir Rufo Jul 13 '19 at 09:18
  • @SirRufo Not Really my real problem is to handle async method that is causing null reference and in above post no explanation for async found – Zia Jul 13 '19 at 09:22
  • 1
    Trust me, it does not matter if async or not - something returns null and you have to debug that fact – Sir Rufo Jul 13 '19 at 09:23
  • M function is exiting before completing the method that is causing my selectlistitem to be null. I want to know how i modify my code so that selectlistitem get filled null exception will be solved – Zia Jul 13 '19 at 09:25
  • Have you registered `MyService` dependency in `Startup`? – Ammar Jul 13 '19 at 10:09
  • Your `_cityrepository` is null. Check your DI registration. – CodeCaster Jul 13 '19 at 10:31
  • Yes my service is registered – Zia Jul 13 '19 at 18:57
  • @CodeCaster your tip helped me and issue is solved. Missed _cityrepository assignment in my constructor. if you would have posted answer i will accept that – Zia Jul 13 '19 at 21:48
  • @Zia You just stated that this is a perfect duplicate and therefore it does not need to be answered - it was answered a long time ago by the duplicate – Sir Rufo Jul 14 '19 at 05:53

0 Answers0