1

Please, I am stuck with this challenge for some hours now. Don’t know where I am getting this wrong.

I have two entities; service_catalog and services. I have a foreign key on services which has a reference to service_catalog. So the idea is that, a service_catalog has many services.

I am trying to get a list of all services based on the service_catalog name passed from the combobox. But I get a null exemption on the serviceList variable. But when I test with a message box, it gave me the id which I taught I could use. But I get the error.

Here is my code. Will appreciate any assistance.

//Get catalog name
private service_catalog GetName(string sCatalogname)
{
    using (Model db = new Model())
    {
        return db.service_catalog.FirstOrDefault(s => s.name == Catalogname);
    }
}

//Load the services based on the service_catalog selected
private void comboBoxServices_SelectedIndexChanged(object sender, EventArgs e)
{
    service_catalog data = GetName(comboBoxServices.Text);
    List<service> serviceList;
    if (data != null)
    {
        serviceList = dbHelper.services.Where(s =>s.service_catalog_id == data.service_catalog_id).ToList();
        dataGridViewServices.DataSource = serviceList;

    }
}

Error Thrown: {"Object reference not set to an instance of an object."} This line:

serviceList = dbHelper.services.Where(s => s.service_catalog_id == data.service_catalog_id).ToList();

I appreciate any assistance.

EDIT
from the comment of the OP

serviceList object count is 0 while data object shows

"{System.Data.Entity.DynamicProxies.service_catalog_BF9EC14C144A9AC84B6F0CB43564996B60E8EA5EA9C2B7D58C0BBE5AE20FC83B}"

Hossein Golshani
  • 1,847
  • 5
  • 16
  • 27
GNG
  • 89
  • 1
  • 8
  • Would you mind adding your exception to your question? – Marco Oct 10 '18 at 13:24
  • Ok, and now the Stacktrace please. Which object is null excactly? Could it be, that you did not initialize `dbHelper`? – Marco Oct 10 '18 at 13:30
  • I initialize dbHelper in my constructor. – GNG Oct 10 '18 at 13:33
  • Ok, put a breakpoint on that line and then check which object is null. – Marco Oct 10 '18 at 13:35
  • serviceList object count is 0 while data object shows "{System.Data.Entity.DynamicProxies.service_catalog_BF9EC14C144A9AC84B6F0CB43564996B60E8EA5EA9C2B7D58C0BBE5AE20FC83B}" – GNG Oct 10 '18 at 13:38
  • 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) – Marco Oct 10 '18 at 13:38
  • Please put any important info in your question not in in a comment. I did it for you this time – GuidoG Oct 10 '18 at 15:47
  • Noted with thanks. – GNG Oct 10 '18 at 16:49
  • could it be your "dbHelper.services" is null, checkout if you initialized your "dbHelper.services" collection in your dbHelper constructor like services = new List() ? – LeY Oct 10 '18 at 23:52

0 Answers0