c# - Get Object reference not set to an instance of an object error from static member -
i have static class this:
public static class applicationlist { public static list<applicationsmodel> applicationsmodels { get; set; } }
i want use applicationsmodels in class
class program { static void main(string[] args) { getapplicationnamefromappconfigandaddtoapplicationlist(); } private static void getapplicationnamefromappconfigandaddtoapplicationlist() { list<string> applicationname = configurationmanager.appsettings["appname"].split(',').tolist(); foreach (var variable in applicationname) { applicationlist.applicationsmodels.add(new applicationsmodel { datetime = datetime.minvalue, name = variable }); } } }
but applicationlist null , error
object reference not set instance of object.
this static prpoerty , can't create instance form static prpoerty
well have
public static list<applicationsmodel> applicationsmodels { get; set; }
property not initialized.
can this, instead:
public static class applicationlist { private static list<applicationsmodel> appmodel = new list<applicationsmodel>(); public static list<applicationsmodel> applicationsmodels { { return appmodel ;} } //don't think need set in case.. // add it, if need }
Comments
Post a Comment