Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The Code Explains it all. i have tried getting the _dealer.Name in forEach loop without using the Predicate and it works fine. But with Predicate ON it starts throwing this Exception


C#
private void btnDealerLoadTockets_Click(object sender, EventArgs e)
        {
            // MainListBox.DataSource = LoadDealersList();
            // And Yes , there are items in the DealersList. dlist.Count returns 5 
    
            List<Dealer> dlist = LoadDealersList();
            Dealer _Dealer = dlist.Find(x => x == (Dealer)MainListBox.SelectedItem);
            foreach (Dealer item in dlist)
            {
                if (item.name == _Dealer.name)
                {
                    Console.WriteLine("Match Found");
                }
            }
        }


What I have tried:

The Code Explains it all. i have tried getting the _dealer.Name in forEach loop without using the Predicate and it works fine. But with Predicate ON it starts throwing this Exception
Posted
Updated 29-Jul-16 4:24am
Comments
F-ES Sitecore 29-Jul-16 9:09am    
What line is the exception on?
Suvabrata Roy 29-Jul-16 9:53am    
How you have bind the MainListBox? because you try to cast the selected item to Dealer type.
[no name] 29-Jul-16 10:27am    
Does LoadList() really return lists for ListBox _and_ ButtonClick, build on the same Dealer reference ?
This because in x => x == (Dealer)MainListBox.SelectedItem compares the two references.

In other words: In case the Content of the list (the Dealers) will be created new while getting the list you will have such a Problem. And this even when the props of the Dealers of both lists are exactly the same...but the references are not.

You should learn to use the debugger ASAP. Set a breakpoint on the first line of your method, and step through it line by line, examining the values of the variables. You'll quickly see which one is null when you don't expect it to be.


The most likely cause is that the Find method is returning null, so you get a NullReferenceException when you try to access a property on the _Dealer variable. The simple solution is to test the variable before you try to use it.
C#
if (_Dealer != null)
{
    foreach (Dealer item in dlist)
    {
        if (item.name == _Dealer.name)
        {
            Console.WriteLine("Match Found");
        }
    }
}
 
Share this answer
 
If your goal here is to determine if there is a match between item.name and a Dealer.name in the List<Dealer> ... how about:
C#
if(dlist.Any(dlr => dlr.name == item.Name)
{
   // whatever
}

// if you need to do something with the match:

Dealer match = dlist.FirstOrDefault(dlr => dlr.name == item.name);

// default for Reference Types is 'null
if(match == null}
{
    // whatever
}
else
{
    // use the match for something ?
}
Really need to know the error message to figure out what's happening with 'Find with Predicate.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900