0

// Updated Code. d = (Dealer)MainListBox.SelectedItem

 private void btnDealerLoadTockets_Click(object sender, EventArgs e)
    {
        List<Dealer> dlist = LoadDealersList();
        Dealer d = (Dealer)MainListBox.SelectedItem;
         d = dlist.Find(x => x == d);
        for (int i = 0; i < dlist.Count; i++)
        {
            Console.WriteLine(d.name);
        }
    }
T-X
  • 103
  • 1
  • 8
  • _d = dlist.Find(x => x == d);_ will never find d – Steve Jul 29 '16 at 11:34
  • Make you mind clear. What are searching for in the dlist variable? What if the search doesn't return a match? – Steve Jul 29 '16 at 11:41
  • my apologies for being stupid. ive got a list and a few members in it. what i wanna do is find an object in it. P.S. its the first time im using Predicates – T-X Jul 29 '16 at 11:43
  • All right but you don't answer my request. What are you searching for in that list?. You can't simply create a Dealer object ex-novo and then try to find it in the list. It is not there, you have just created it. Probably you are searching the list with a specific instance with some kind of identifying property and, if found display the _name_ property of the found instance. – Steve Jul 29 '16 at 11:47
  • yes, the Object im trying to search is taken from (Dealer)listbox1.SelctedItem. where ListBox1's datasource is the same Dealers list. its not written in the code above. stupid mistake – T-X Jul 29 '16 at 11:51

1 Answers1

0

Check your dlist before applying any methods on it. There is no explicit List initialization in your code, make it sure your List was initialized.

Null Reference Exception tells you that your collection was initialized

ams4fy
  • 185
  • 1
  • 7