如何在不跳过合法循环的情况下处理nullreferenceexception?

时间:2013-09-30 06:33:18

标签: c# .net sharepoint

我有以下代码:

foreach (SPListItem item in list.Items)
{

    string itemId = item.ID.ToString();
    string contentType = item.ContentType.ToString();
    string displayName = item.DisplayName;
    string name = item.Name;

    // todo: Title not always retreiving? Likely due to folders, needs more handling
    //string title = item["Title"].ToString() ?? null;
    string title = "";

    string url = item.Url;
    string author = item["Created By"].ToString();

    // todo: Checked out files catering
    const string checkedOutBy = "";

    DateTime lastModified = Convert.ToDateTime(item["Modified"]);
    string lastModifiedBy = item["Modified By"].ToString();
    DateTime created = Convert.ToDateTime(item["Created"]);


    query.RecordItems(itemId, contentType,
                        displayName, name,
                        title, url, author,
                        checkedOutBy,
                        lastModified,
                        lastModifiedBy,
                        created,
                        author);
}

我遇到的问题是,在循环的一些迭代中,Title或ContentType将抛出Nullreferenceexception,但不会抛出所有这些异常。我相信我已经满足以下要求了,但我不确定 - 有更好的方法吗?

foreach (SPListItem item in list.Items)
{
    try
    {
        string itemId = item.ID.ToString();
        string contentType = item.ContentType.ToString();
        string displayName = item.DisplayName;
        string name = item.Name;

        // todo: Title not always retreiving? Likely due to folders, needs more handling
        //string title = item["Title"].ToString() ?? null;
        string title = "";

        string url = item.Url;
        string author = item["Created By"].ToString();

        // todo: Checked out files catering
        const string checkedOutBy = "";

        DateTime lastModified = Convert.ToDateTime(item["Modified"]);
        string lastModifiedBy = item["Modified By"].ToString();
        DateTime created = Convert.ToDateTime(item["Created"]);


        query.RecordItems(itemId, contentType,
                            displayName, name,
                            title, url, author,
                            checkedOutBy,
                            lastModified,
                            lastModifiedBy,
                            created,
                            author);
    }
    catch (NullReferenceException ex)
    {
        Logger.Error("[{0}] Filed moving on file {1} as not all content was present", item.Name);
    }

}

2 个答案:

答案 0 :(得分:2)

希望我真的理解这里的问题,为什么不呢......

foreach (SPListItem item in list.Items)
{
   ...... 
   if(title == null || item.ContentType == null) 
       continue;


   ....
}

如果这不是您所要求的,请澄清。

答案 1 :(得分:1)

您可以过滤项目并按以下方式循环

list = list.Items.Where(i=> i.ContentType !=null && i["Title"] !=null).ToList();

如果您不需要执行上述所有验证和过滤器,最好转到for loop

for (int i = 0; i < list.Items.Count; i++)
{
    try
    {
       SPListItem item = list.Items[i];
       // your code 
    }
    catch (NullReferenceException ex)
    {
        Logger.Error("[{0}] Filed moving on file {1} as not all content was present", item.Name);
    }

}