Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

'Object reference not set' error, but list has 27 references in C# .Net

I’m using Visual Studio 2019 and I’m getting this error when I’m trying to populate a list. My coworker tried to help me and thought it was strange to get the following error since my list has 27 references to objects, so part of it is being recognized but part of it isn’t:

An unhandled exception occurred while processing the request. NullReferenceException: Object reference not set to an instance of an object.

Here is some of the models:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public class WithholdingViewModel
{
    public List<County> Counties { get; set; }
    public SelectList CountySelectList { get; set; }
}

Here is where the class for it is in the model:

public class County
{
    public string CountyName { get; set; }
    public double CountyRate { get; set; }

    public County(string CountyName, double CountyRate)
    {
        this.CountyName = CountyName;
        this.CountyRate = CountyRate;
    }

    public County()
    {
    }
}

Here is the beginning of the method where I try to populate the list in the model:

public void createAndPopulateCounties()
{
    Counties.Add(new County(CountyName = "Allegany", CountyRate = 0.0305));
}

And the controller where the method is called (I commented out the select list since that doesn’t work either):

[HttpGet]
public IActionResult Index()
{
    WithholdingViewModel model = new WithholdingViewModel();

    // Call the method to create and populate the list of counties
    model.createAndPopulateCounties();

    // model.CountySelectList = new SelectList(model.Counties, "CountyName", "CountyName");

    return View("Index", model);
}

My coworker and I tried changing the code in the County class that said this.CountyName = CountyName; a few different ways, I used to have code like this in my function

List<County> Counties = new List<County>();

but that was a problem since it wasn’t populating the original list, it was creating a second one (my coworker noticed that). I can’t figure this out from the other stack overflow postings, and none of the other postings mention references to the object. I tried to upload an image of the references, but StackOverflow said it was forbidden. But it does reference the County objects I tried to add, but then I get the runtime error.

>Solution :

Since you aren’t passing any arguments to the constructor for your model, you could try initializing the list when you initialize the model like:

WithholdingViewModel model = new WithholdingViewModel{
    Counties = new List<County>()
};

or, if you have a list of County object already, can do:

List<County> myCountyListFromEarlier = someMethodToBuildTheList();

WithholdingViewModel model = new WithholdingViewModel{
    Counties = myCountyListFromEarlier
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading