在我的 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
最佳答案
当 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/