Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm having a problem with the List<SelectListItem>. Whenever the code hits the foreach it says:

object reference not set to an instance of an object.

Am I missing something or can anyone explain why it is failing there?

public ActionResult HammerpointVideos(string category, string type)
{
    var stuff = Request.QueryString["category"];

    var ItemId = (from p in entities.EstimateItems
                  where p.ItemName == category
                  select p.EstimateItemId).FirstOrDefault();

    var Videos = (from e in entities.EstimateVideos
                  where e.EstimateItemId == ItemId
                  select new Models.HammerpointVideoModel
                  {
                      VideoName = e.VideoName,
                      VideoLink = e.VideoLink
                  }).ToList();

    var model= new Models.HammerpointVideoListModel();
    List<SelectListItem> list = model.VideoList;

    foreach (var video in Videos)
    {
        list.Add(new SelectListItem()
                    {
                        Selected=false,
                        Value = video.VideoLink,
                        Text = video.VideoName
                    });

    }
}
share|improve this question
    
What does your model class look like? Are you initializing VideoList? –  DGibbs Jun 18 '13 at 12:43
    
What's the exact line where error triggers? –  Claudio Redi Jun 18 '13 at 12:44
    
Yes, I assumed he meant Videos was null, also, is it? Debug! –  Mike C. Jun 18 '13 at 12:45
    
the only thing I see is that your linq is asking where e.EstimateItemId == ItemId...but ItemId isn't an int type, it's an unspecified var. I have no idea if that is the issue, but if it is, then the reason your code doesn't work is because it can't distinguish the VALUE of ItemId, and so the second linq block returns no results. I may be way off however. Other people? Is this a possibility? I haven't touched LINQ in a year, so I'm a bit rusty there. –  Russell Uhl Jun 18 '13 at 12:46

2 Answers 2

up vote 3 down vote accepted

Is ViedoList initialized before? I assume it is not. Create new list add items to it and after that add reference to it in your model:

var model = new Models.HammerpointVideoListModel();
List<SelectListItem> list = new List<SelectListItem>();

foreach (var video in Videos)
{
    list.Add(new SelectListItem()
                {
                    Selected=false,
                    Value = video.VideoLink,
                    Text = video.VideoName
                });

}

model.VideoList = list;
share|improve this answer

Probably You didn't initialized VideoList in the parameterless constructor of HammerpointVideoListModel class, so it is NOT an empty list.

Put

VideoList = new List<SelectedListItem>();

in the constructor.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.