adding object to list of objects
I'm getting an exception and I'm not seeing what I'm missing.
I have an object model called MyCom开发者_如何学运维posedModel and I'm creating a list of these by adding each element as they're built.
I have this:
List<MyComposedModel> TheListOfModel = null;
MyComposedModel ThisObject = new MyComposedModel();
foreach (MyComposedModel in some list)
{
ThisObject.Reset() //clears all properties
....
TheListOfModel.Add(ThisObject)
}
And I'm getting Object reference not set to an instance of an object.
on the Add(ThisObject) line.
What am I missing?
Thanks.
You have to initialize TheListOfModel
first. Instead of:
List<MyComposedModel> TheListOfModel = null;
do this:
List<MyComposedModel> TheListOfModel = new List<MyComposedModel> ();
TheListOfModel is null, so you can't apply a method on it.
精彩评论