【问题标题】:(SPFieldLookupValue) splistitem of Lookup type throws Object reference not set to an instance of an object exception(SPFieldLookupValue) Lookup 类型的 splistitem 抛出 Object reference not set to an instance of an object exception
【发布时间】:2012-01-02 03:15:26
【问题描述】:

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

对象引用未设置为对象的实例。

此错误仅出现在未填写任何值的查找字段上。我尝试使用SPFieldLookupValue 来检查空值,但我仍然收到错误。

这就是我检查空值的方式:

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

有什么帮助吗?

【问题讨论】:

    标签: sharepoint sharepoint-2010 sharepoint-2007


    【解决方案1】:

    当我开始使用 SP2010 时,我遇到了类似的问题。这个帖子有the answer to your problem

    【讨论】:

    • 嗨,克里斯,非常感谢您的帮助。这非常有用。问候,
    【解决方案2】:

    你得到这个异常的原因在这里:listItem[columnDisplayName].ToString() 因为listItem[columnDisplayName] 没有值并返回 null 你试图在 null 对象上调用 ToString() 所以它抛出“对象引用未设置为对象的实例例外”。

    如果您只是想检查 item 字段是否不为空,请这样做:

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

    【讨论】:

    • 嗨,亚历山大,非常感谢您的回复。我一直盯着它看,却没有意识到我没有检查字段项的空值。在其他代码中,我曾经检查字段项是否为空,但这次我依靠 SPFieldLookUpValue 认为这是检查空值的正确方法。再次感谢您的大力帮助。
    • 最有可能的是,将null 传递给SPFieldLookupValue 是导致异常的原因; “未设置对象引用...”是 SharePoint 异常,而不是 JavaScript 内置异常。 (调用 null.toString() 会导致单独的“未捕获的类型错误:无法读取属性 'toString' of null”。)
    猜你喜欢
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多