c# - Get "Object reference not set to an instance" when trying to add element to list -
i have created model;
namespace gantt.models { public class ganttmodels { public ilist<ganttmodel> allgantt { get; set; } } public class ganttmodel { public string projectname { get; set; } public ienumerable<resourcesset> rescource { get; set; } } }
now plan add items model, have done in repository this;
namespace gantt.models { public class gantdatarepository { gantentities dbcontext = new gantentities(); ganttmodels returnmodels = new ganttmodels(); ganttmodel tempganttmodel = new ganttmodel(); public gantdatarepository() { foreach (var item in dbcontext.workplans) { tempganttmodel.projectname = item.product; tempganttmodel.rescource = item.resourcessets; returnmodels.allgantt.add(tempganttmodel); // here error } } public ganttmodels getgant() { return returnmodels; } } }
the repositor finds data , add it. see have instansiate returnmodels already
returnmodels
initialized.
returnmodels.allgantt
not.
you can this:
public class ganttmodels { public ilist<ganttmodel> allgantt { get; set; } public ganttmodels() { allgantt = new list<ganttmodel>(); } }
or something.
Comments
Post a Comment