C# 如何在不跳过合法循环的情况下处理nullreferenceexception?
我有以下代码:C# 如何在不跳过合法循环的情况下处理nullreferenceexception?,c#,.net,sharepoint,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 re
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);
}
}
希望我能真正理解这里的问题,那么为什么不
foreach (SPListItem item in list.Items)
{
......
if(title == null || item.ContentType == null)
continue;
....
}
如果这不是你想要的,请澄清。希望我真的能理解这里的问题,那么为什么不
foreach (SPListItem item in list.Items)
{
......
if(title == null || item.ContentType == null)
continue;
....
}
如果这不是您想要的,请澄清。您可以按如下方式筛选项目并循环它们
list = list.Items.Where(i=> i.ContentType !=null && i["Title"] !=null).ToList();
如果您不需要执行上述所有验证和筛选,最好转到for循环
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);
}
}
for(int i=0;i
您可以按如下方式筛选项目并循环它们
list = list.Items.Where(i=> i.ContentType !=null && i["Title"] !=null).ToList();
如果您不需要执行上述所有验证和筛选,最好转到for循环
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);
}
}
for(int i=0;i
相关代码将抛出item.ContentType.ToString()
因此您应该选中item.ContentType
而不是contentype
相关代码将抛出item.ContentType.ToString()
所以你应该选择item.ContentType
而不是contentype
SPListItem item=list.Items[i]代码>否。您不在SharePoint中执行此操作,因为它不会缓存列表。项目
,但会每隔一天从服务器获取列表time@DmitryDovgopolyOP未提供有关列表的详细信息。它可能已经是缓存列表了,您认为这是此异常的根源吗?显然,它不是异常的来源,但使用SPList.Items
,这是一种非常不推荐的方法<代码>列表
是SPList
,因为在此上下文中它不能是任何其他内容。@DmitryDovgopoly请回答此问题并提供您的解决方案。肯定会有帮助的。谢谢你的评论。我认为@Tigran答案是正确的,我已经添加了指向SharePoint最佳实践的链接作为评论。没有什么可补充的。您的答案是可以的,但SharePoint有时会很棘手。SPListItem item=list.Items[i]代码>否。您不在SharePoint中执行此操作,因为它不会缓存列表。项目
,但会每隔一天从服务器获取列表time@DmitryDovgopolyOP未提供有关列表的详细信息。它可能已经是缓存列表了,您认为这是此异常的根源吗?显然,它不是异常的来源,但使用SPList.Items
,这是一种非常不推荐的方法<代码>列表
是SPList
,因为在此上下文中它不能是任何其他内容。@DmitryDovgopoly请回答此问题并提供您的解决方案。肯定会有帮助的。谢谢你的评论。我认为@Tigran答案是正确的,我已经添加了指向SharePoint最佳实践的链接作为评论。没有什么可补充的。您的答案是可以的,但SharePoint有时会很棘手。请提供一些SharePoint最佳实践。例如,不要枚举整个SPList.Items集合
请遵循一些SharePoint最佳做法。例如,不枚举整个SPList.Items集合