sharepoint - (SPFieldLookupValue) Lookup 类型的 splistitem 抛出未设置对象实例的对象引用异常

标签 sharepoint sharepoint-2010 sharepoint-2007

我有一个共享点列表,其中包含一些查找字段。当我迭代代码中的项目时,出现以下错误:

Object reference not set to an instance of an object.

仅当查找字段未填写任何值时,才会出现此错误。我尝试使用 SPFieldLookupValue 检查空值,但仍然收到错误。

这是我检查空值的方法:

SPFieldLookupValue value = new SPFieldLookupValue(listItem[columnDisplayName].ToString()); 
if (value.LookupValue != null)

大家有什么帮助吗?

最佳答案

您收到此异常的原因在于:listItem[columnDisplayName].ToString()因为listItem[columnDisplayName]没有值并返回您尝试调用的null ToString() 在空对象上,因此它抛出“对象引用未设置到对象异常的实例”。

如果您只是想检查项目字段是否不为空,那么就这样做:

if (listItem[columnDisplayName]!=null) 
{
    //here you can access listItem[columnDisplayName] safely
}

关于sharepoint - (SPFieldLookupValue) Lookup 类型的 splistitem 抛出未设置对象实例的对象引用异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8197984/

相关文章:

c# - Sharepoint 服务将子站点从私有(private)站点复制到另一个站点

sharepoint - 使用 Sharepoint API 确定 SharePoint 应用程序池帐户?

sharepoint - 使用 Microsoft Graph API 获取 SharePoint 文件夹和文档

c# - 从 SharePoint 字段选择列中检索所有项目

css - CSS 规则可以动态添加到 Sharepoint 2010 Web 部件控件吗?

Sharepoint 库项目 View - 项目计数以及查看该项目的人员

sharepoint - 以编程方式在文档库中搜索特定文档

sharepoint - checkin 和 checkout SPFile 对象

不带架构的 sharepoint 数据 View Web 部件 xslt

.net - SharePoint 开发环境设置