本文介绍了奇怪的空引用异常。使用谓词时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

守则解释了这一切。我试过在forEach循环中获取_dealer.Name而不使用谓词,它工作正常。但是使用Predicate ON它会开始抛出这个例外





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


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");
                }
            }
        }





我尝试了什么:



守则解释了这一切。我试过在forEach循环中获取_dealer.Name而不使用谓词,它工作正常。但是使用Predicate ON它会开始抛出此异常



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

推荐答案

if (_Dealer != null)
{
    foreach (Dealer item in dlist)
    {
        if (item.name == _Dealer.name)
        {
            Console.WriteLine("Match Found");
        }
    }
}


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 ?
}

真的需要知道错误信息,以找出'Find with Predicate。

Really need to know the error message to figure out what's happening with 'Find with Predicate.


这篇关于奇怪的空引用异常。使用谓词时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 21:19