Click here to Skip to main content
15,917,808 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.
C#
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;
    }
}
Posted
Updated 6-Oct-10 9:15am
v3
Comments
Hiren solanki 6-Oct-10 8:27am    
can you spot the error by putting debug point ?
Kasunmit 6-Oct-10 8:33am    
hi done as u said pls help

1 solution

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.
 
Share this answer
 
v2

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