locked
Null Reference Exception RRS feed

  • Question

  • User-420597931 posted

    Hi,

    I am trying to populate my dropdown list from my json file which is in contentroot. I'm using asp.netcore but I've this MVC as well.

    MY model class:

    public class Registration

    {

    public string Country{get;set;};

    public List<SelectListItem> CountryDropDownList{get;set;}

    }

    My controller Code-

    Public class AccountController:Controller

    {

    private readonly IWebHostEnvironment _env;

    Public AccountController(IWebHostEnvironment env){

    _env = env;

    }

    public void OnGet(Registration registration)
            {
                registration.CountryDropDownList = GetCountryItems();
            }
            public List<SelectListItem> GetCountryItems()
            {
                string filepath = Path.Combine(_env.ContentRootPath, "CountryList.json");
                string jsonlist = System.IO.File.ReadAllText(filepath);
                var result = JsonConvert.DeserializeObject<RootCountry>(jsonlist);
                List<SelectListItem> _countrydropdownlist = new List<SelectListItem>();
                foreach (var nation in result.Countries)
                {
                    _countrydropdownlist.Add(new SelectListItem { Value = nation.Code.ToString(), Text = nation.Name });
                }
                return _countrydropdownlist;
            }

    }

    My view-

    <div class="col-md-6">
                            <div class="form-group">
                                <label asp-for="Country"></label>
                                <select asp-for="Country" asp-items="@Model.CountryDropDownList">
                                    <option selected disabled> --- Select Country --- </option>
                                </select>
                                <span asp-validation-for="Country" class="text-danger"></span>
                            </div>
                        </div>

    I've tried checking if I'm going to right path and filled a text file with what I'm getting to see if the path is correct.
    So I think the problem is somewhere in my Void Onget method.

    I have taghelpers in viewimports as well.
    Any pointers is appreciated.

    Thank you.

    Monday, November 9, 2020 8:45 AM

All replies

  • User1120430333 posted

    The error message means that code in your program is trying to access an object that does not exist in memory a null valued object.

    OO IS OO .NET or Java. Its OO 101.

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    You should try to use the visual stuido  debugger and find the line that is accessing an object that is null meaning it does not exist.  You can use the debugger's Quickwatch.

    Monday, November 9, 2020 9:27 AM
  • User-420597931 posted

    The error message means that code in your program is trying to access an object that does not exist in memory a null valued object.

    OO IS OO .NET or Java. Its OO 101.

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    You should try to use the visual stuido  debugger and find the line that is accessing an object that is null meaning it does not exist.  You can use the debugger's Quickwatch.

    Hy DA924,

    Thank you for your reply. The problem here is as soon as I click to go to my register view, Iget this null error even though I've put this breakpoint in my controller. It does not get hit.

    Tuesday, November 10, 2020 6:07 AM
  • User1120430333 posted

    DA924

    The error message means that code in your program is trying to access an object that does not exist in memory a null valued object.

    OO IS OO .NET or Java. Its OO 101.

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    You should try to use the visual stuido  debugger and find the line that is accessing an object that is null meaning it does not exist.  You can use the debugger's Quickwatch.

    Hy DA924,

    Thank you for your reply. The problem here is as soon as I click to go to my register view, Iget this null error even though I've put this breakpoint in my controller. It does not get hit.

    The exception message didn't just show by itself. The exception message should have been displayed in an exception popup box that should expose the line of code throwing the exception. 

    https://michaelscodingspot.com/debugging-exceptions/

    Tuesday, November 10, 2020 6:53 AM
  • User-420597931 posted

    THank you. I can now see the error.
    It is CS1525: Invalid expression term'<'.

    This is in <select asp-for="Country" asp-items="@Model.CountryDropDownList">

    Tuesday, November 10, 2020 8:51 AM
  • User711641945 posted

    Hi Sanjok,

    What is your whole error message?Did your dropdownlist populate data?

    Best Regards,

    Rena

    Friday, November 27, 2020 8:11 AM
  • User-420597931 posted

    Hi Sanjok,

    What is your whole error message?Did your dropdownlist populate data?

    Best Regards,

    Rena

    Hy Rena,

    I've already solved this issue. 

    Thank you.

    Friday, November 27, 2020 8:17 AM