c# populating view model

I am trying to populate a ViewModel and I am getting an "Object reference not set to an instance of an object." when trying to populate a List within my main view model. See my two ViewModels and my code to populate them.

As you can see I am building a List of InventoryWorkTypesMSViewModel from a common delimited field of WorkTypes in the InventoryViewModel. Am I doing this wrong? If so what should I do to fix it?

public class InventoryViewModel
    {
        public int InventoryId { get; set; }
        public int ProjectId { get; set; }
        public int Qty { get; set; }
        public bool AuditFlag { get; set; }
        public string UpdatedBy { get; set; }
        public DateTime? UpdatedDt { get; set; }
        public int? ProductNameId { get; set; }
        public string Size { get; set; }
        public string WorkTypes { get; set; }
        public List<InventoryWorkTypesMSViewModel> WorkTypesMS { get; set; }

public class InventoryWorkTypesMSViewModel
    {
        public string WorkType { get; set; }
    }

public List<InventoryViewModel> GetList(int projectId, int? productNameId)
        {
                var model = _db.Inventories
                        .Select(i =>
                              new InventoryViewModel
                              {
                                  InventoryId = i.InventoryId,
                                  ProjectId = i.ProjectId,
                                  Qty = i.Qty,
                                  AuditFlag = i.AuditFlag,
                                  ProductNameId = i.ProductNameId,
                                  UpdatedBy = i.UpdatedBy,
                                  UpdatedDt = i.UpdatedDt,
                                  Size = i.Size,
                                  WorkTypes = i.WorkTypes
                              })
                        .ToList();

                foreach(var m in model)
                {
                    if(m.WorkTypes != null)
                    {
                        string[] workTypeArray = m.WorkTypes.Split(',');

                        foreach(string wt in workTypeArray)
                        {
                            m.WorkTypesMS.Add(new InventoryWorkTypesMSViewModel
                            {
                                WorkType = wt
                            });
                        }
                    }
                }

                return model;
        }

Open in new window

nmiller61Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Your WorkTypeMS property is never initiated, so it's null, and you can't add to a null. You should initialise the property before using it:

public List<InventoryWorkTypesMSViewModel> WorkTypesMS { get; set; } = new List<InventoryWorkTypesMSViewModel>();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deleydCommented:
The possible places I see offhand are:
1. Test that the model returned from _db.Inventories isn't null before using it.
2. m.WorkTypesMS.Add(...) can we be assured that m.WorkTypesMS isn't null? (I can't tell from here. It might be OK.)
3. foreach(var m in model) probably OK, probably don't need to check that m is null, unless model is a list which might contain null entries.

Probably likely problem I'll guess is #1.
after var model =
add a line:
if (model == null)
{
    do something...
}

Open in new window

0
nmiller61Author Commented:
Thanks. I figured it was something simple I missed.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET MVC

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.