This question already has an answer here:
- What is a NullReferenceException, and how do I fix it? 31 answers
Hi guys I'm receiving an error: "Object reference not set to an instance of an object". I'm not quite sure why... Here's the code:
public void LoadUserContacts(ListBox FriendsLb)
{
FriendsLb.DisplayMember = "Display";
var query = from o in Globals.DB.Friends
where o.UserEmail == Properties.Settings.Default.Email
select new
{
FirstName = o.FirstName,
LastName = o.LastName,
Email = o.Email,
Display = string.Format("{0} {1} - ({2})", o.FirstName, o.LastName, o.Email),
};
FriendsLb.DrawMode = DrawMode.OwnerDrawVariable;
foreach (object contact in query.ToList())
{
string details = query.GetType().GetProperty("Display").ToString();
FriendsLb.Items.Add(new Contacts(Properties.Resources.avatar, details));
FriendsLb.DrawItem += FriendsLb_DrawItem;
FriendsLb.MeasureItem += FriendsLb_MeasureItem;
}
}
Which code is causing the error:
string details = query.GetType().GetProperty("Display").ToString();
Any ideas? I'm trying to get the display property from the query:
Display = string.Format("{0} {1} - ({2})", o.FirstName, o.LastName, o.Email),