【发布时间】:2012-01-02 15:45:39
【问题描述】:
我在运行程序时不断收到此错误。
对象引用未设置为对象的实例。 说明:执行当前 Web 请求期间发生未处理的异常。 请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。 异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。
Source Error:
Line with error:
Line 156: if (strSearch == "" || strSearch.Trim().Length == 0)
正确的写法应该是什么?
【问题讨论】:
-
添加大小写 if(strSearch != null)?
-
错误指示对象为空。 strSearch 为 null 或 strSearch.Trim() 的结果为 null。要检查,请使用 Graphain 的答案,使用 IsNullOrWhitespace。
标签: c#