asp.net - 值 = 'p.Value' 抛出类型为 'System.NullReferenceException' 的异常

标签 asp.net umbraco

在我的 umbraco 网站中,我得到了这样的代码

 var  p = currentNode.GetProperty("ucc") as Property;
 if (p != null && !string.IsNullOrEmpty(p.Value.Trim()))
 mailCC = p.Value;

但总是抛出这样的错误

Value = 'p.Value' threw an exception of type 'System.NullReferenceException'

注意:我确定 P.Value 为 note Null enter image description here

最佳答案

p.Value 为 null 时调用 Trim() 方法会引发错误。在您的代码中,这是在 string.IsNullOrEmpty 执行检查之前发生的。

将您的表达式修改为以下内容应该可以修复它。

代码:

var p = currentNode.GetProperty("ucc") as Property;
if (p != null && !string.IsNullOrWhiteSpace(p.Value))
    mailCC = p.Value

引用:

String.IsNullOrWhiteSpace : Indicates whether a specified string is null, empty, or consists only of white-space characters.

关于asp.net - 值 = 'p.Value' 抛出类型为 'System.NullReferenceException' 的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15338592/

相关文章:

c# - 在 ASP.NET 单元测试中模拟 HttpContext.server.MapPath

c# - 将剧院座位保存到数据库的最快方法

html - 水平对齐 div 和标签

css - 具有不同字体类型的多语言网站

c# - Umbraco 新手,我应该从哪里开始?

c# - 如何在 C# 中检查 SQL SqlDataSource 中的零行?

c# - 使用 UNION、INTERSECT 或 EXCEPT 运算符组合的所有查询在其目标列表中必须具有相同数量的表达式

umbraco - 如何在 Umbraco CMS 中创建 "sidebar"?

asp.net - Umbraco 7:无法加载Umbraco.Web.UmbracoApplication类型

Umbraco 7 自定义成员(member)提供商同时使用 Umbraco 用户和外部成员(member)