零期望 [英] null expection

查看:94
本文介绍了零期望的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.我编写了以下方法来对DataGrid中的数据进行分类,但是调用该方法时会抛出NullReferenceException.
请帮帮我.
DataGrid的主题"列包含数据.调试点突出显示.

Hi. I wrote the following method for classifying data in the DataGrid, but when calling that method it''s throwing NullReferenceException.
Please help me.
The Subject Column of the DataGrid is having data. Debug point is highlighted.

public void FilterFromSubject(string mailSubject)
{
    List<UnreadEmails> list = (List<UnreadEmails>)dgvUnreadMails.DataSource;
    List<UnreadEmails> filteredList = new List<UnreadEmails>();

    List<string> myUnreadMailList;
    try
    {
        filteredList = (List<UnreadEmails>)(from ci in list
                                            where ci.Subject.ToLower().Contains(mailSubject)
                                            select ci).ToList<UnreadEmails>();
        dgvUnreadMails.DataSource = filteredList;
        btnCancel.Enabled = true;
    }
    catch (System.Exception)
    {
        MessageBox.Show("Import E-mails first", "Empty Table", MessageBoxButtons.OK, MessageBoxIcon.Information);
        btnSave.Enabled = false;
    }
}

推荐答案

好吧,第一行是从某处的方法获取列表,而您只是假设返回的列表是a)不null,并且b)有项目.

我将if (list != null)放在第一行之后,并且仅在有理由的情况下才执行进一步的处理.
Well, the first line is getting the list from a method somewhere, and you''re just assuming thast the returned list is a) not null, and b) has items.

I would put if (list != null) after the first line, and only perform further processing if there''s a reason to do so.


这篇关于零期望的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆