0

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),
Reza Aghaei
  • 103,774
  • 12
  • 145
  • 300
richardj97
  • 79
  • 9
  • 1
    It is not a duplicate, the linked question doesn´t have quite the same problem, since the problem here is a misunderstanding of how to write the code and what parts of it does. – Mikael Puusaari Jul 03 '16 at 18:59
  • 1
    Type of the query is `System.Linq.Enumerable.WhereSelectEnumerableIterator` which `T` is type of object which you use in `from`, and `K` is type of anonymous object which you created in select. So `query.GetType().GetProperty("Display")` returns null, because there is no "Display" property in the returned type and then calling `ToString()` on it will raise a null reference exception. To solve the problem using reflection follow what is said by @MikaelPuusaari, get property from item of the loop. – Reza Aghaei Jul 03 '16 at 19:10
  • 2
    **But you should know you don't need reflection to get a value from items of a query.** Just use a simple `foreach` loop this way: `foreach (var item in query) { string details = item.Display; }` – Reza Aghaei Jul 03 '16 at 19:10
  • Don't edit your question to add the solution. You have chosen accepted answer and it's enough. – Reza Aghaei Jul 03 '16 at 20:55

1 Answers1

0

You are trying to get the type of the query, and then the property of "Display" of the type, not the return value of the column in the database

The type of the query will not have that the property "display". You need to do something more like:

string property = contact.GetProperty("Display", typeof(string));

string details = property.Name;

I hope it puts you on the right track.

Also, set a breakpoint at that row to see what part of the line is a null reference to easily pinpoint where where problem is.

Mikael Puusaari
  • 768
  • 1
  • 8
  • 14