-1

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:

public class BogusModel
{
    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()
{
    BogusModel model = new BogusViewModel();

    // 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.

Elissa121
  • 1
  • 5
  • 1
    Have you instantiated the counties list somewhere like this List Counties = new List – Amit Kotha Apr 25 '22 at 18:06
  • 1
    Before adding elements to the counties list you need to instantiate it – Amit Kotha Apr 25 '22 at 18:08
  • I mentioned in the last paragraph that I used to have code like that in my function, but it created a new list that wasn't having the counties added to it, so then there were 2 of the same list. – Elissa121 Apr 25 '22 at 18:12
  • You can do like this in the model itself public List Counties { get; set; } = new List();. No need to create a new object of counties everytime . – Amit Kotha Apr 25 '22 at 18:22

1 Answers1

0

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
};
Justin Pearce
  • 4,994
  • 2
  • 24
  • 37
  • Thank you! I tried to do that and then modified it to this: ``` model.Counties = new List();``` and no more error. When I debug and step through the function adding counties to the list, it does end up showing the list being populated. – Elissa121 Apr 25 '22 at 18:37