0

I am getting an error I can't figure out how to fix? It claims to be happening on:

var respondents = RespondentRepository.GetRespondents(UserSession, fieldsToInclude).Where(r=>r.Email.Contains(query)).ToList();

When I remove all the .Where stuff it appears to work fine. I am not really use to this error so I am not sure where the best start would be?

Error:

System.NullReferenceException: Object reference not set to an instance of an object.

Code:

MakeSafeRequest(() =>
                {
                    var respondents = RespondentRepository.GetRespondents(UserSession, fieldsToInclude).Where(r=>r.Email.Contains(query)).ToList();

                    model = new RespondentSearchViewModel
                    {
                        Respondents = respondents,

                        TableData = respondents.SelectToListOrEmpty(r =>
                        {
                            return new TableRowViewModel
                            {
                                Id = r.Id,
                                Data = new Dictionary<string, string>
                                {
                                    { "FirstName", r.FirstName },
                                    { "LastName", r.LastName },
                                    { "EmailAddress", r.Email },
                                    { "Project", r.ProjectId.ToString() },
                                    { "Status", r.RecruitingStatus.ToIconHtml() },
                                    { "ViewHistory", "<div class=\"btnBlue btSml\"><a class=\"closeMe\" href=\"/RespondentSearch/RespondentDetails/?respondentId="+r.Id +"\" >view detail</a></div>"}
                                    //{ "StatusDate", r.LastActionDate.Value.ToLocalTime().ToString() },
                                }
                            };
                        })
                    };
                });

MODEL:

public class RespondentSearchViewModel:ListPageViewModel
    {
        public List<Respondent> Respondents { get; set; }
    }
5
  • Your method RespondentRepository.GetRespondents is returning null Commented Nov 7, 2013 at 19:06
  • Is there a way to better handle a returned null? Commented Nov 7, 2013 at 19:08
  • Also r.Email might be null. Try changing your lambda to r => (r.Email == null ? false : r.Email.Contains(query)) Commented Nov 7, 2013 at 19:08
  • That worked Thanks so so much! Post that as an answer and I will mark it as the selected answer! Commented Nov 7, 2013 at 19:14
  • Posted as answer. Thank you! Commented Nov 7, 2013 at 19:29

1 Answer 1

1

r.Email might be null. Try changing your lambda to:

r => (r.Email == null ? false : r.Email.Contains(query))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.