【问题标题】:Object reference not set to an instance of an object. [duplicate]你调用的对象是空的。 [复制]
【发布时间】: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#


【解决方案1】:

.NET 4.0 中正确的做法是:

if (String.IsNullOrWhiteSpace(strSearch))

上面使用的String.IsNullOrWhiteSpace方法等价于:

if (strSearch == null || strSearch == String.Empty || strSearch.Trim().Length == 0) 
// String.Empty is the same as ""

IsNullOrWhiteSpace 方法参考

http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx

指示指定字符串是 Nothing、空还是包含 仅限空白字符。

在早期版本中,您可以执行以下操作:

if (String.IsNullOrEmpty(strSearch) || strSearch.Trim().Length == 0)

上面使用的String.IsNullOrEmpty方法等价于:

if (strSearch == null || strSearch == String.Empty)

这意味着您仍然需要按照示例使用.Trim().Length == 0 检查您的“IsWhiteSpace”案例。

IsNullOrEmpty 方法参考

http://msdn.microsoft.com/en-us/library/system.string.isnullorempty.aspx

表示指定的字符串是Nothing还是Empty字符串。

说明:

在使用点字符 (.) 取消引用之前,您需要确保 strSearch(或任何变量)不是 null - 即在您执行 strSearch.SomeMethod()strSearch.SomeProperty 之前,您需要检查strSearch != null

在您的示例中,您要确保字符串具有值,这意味着您要确保字符串:

  • 不为空
  • 不是空字符串(String.Empty/"")
  • 不只是空格

在上述情况下,您必须输入“Is it null?”大小写优先,因此当字符串为null 时,它不会继续检查其他情况(和错误)。

【讨论】:

  • 你错过了 if 的结束 )
  • +1。但这只是 .Net 4.0 或更高版本。
  • 很高兴有一个类扩展方法来做这个检查: public static IsEmptyOrWhiteSpace(this string value) { return String.IsEmptyOrWhiteSpace(value);
  • 您能帮我解决我也遇到的问题吗?
  • 当然——你需要知道什么?
【解决方案2】:

在这种情况下,strSearch 可能为空(而不是简单地为空)。

尝试使用

String.IsNullOrEmpty(strSearch)

如果您只是想确定字符串是否没有任何内容。

【讨论】:

  • 如果他的字符串包含空格,这将无济于事。
  • 最初的问题是寻找一个长度为 0 的字符串,所以严格来说这将是一个稍微不同的要求。
  • @Handsome Cam:现在我在这条线上遇到了错误。我认为它的措辞不正确。字符串 strSearch = Request["txtSearchName"].ToString();
  • @Handsome - 调用 Trim() 后的 0 长度字符串。
  • @RitchMelton 哈哈!这就是我因速度误读而得到的!
【解决方案3】:

.Net 的所有版本:

if (String.IsNullOrEmpty(strSearch) || strSearch.Trim().Length == 0)

.Net 4.0 或更高版本:

if (String.IsNullOrWhitespace(strSearch))

【讨论】:

    【解决方案4】:

    我想通过说您可以为此功能创建扩展方法来扩展 MattMitchell 的答案:

    public static IsEmptyOrWhitespace(this string value) {
        return String.IsEmptyOrWhitespace(value);
    }
    

    这样就可以调用:

    string strValue;
    if (strValue.IsEmptyOrWhitespace())
         // do stuff
    

    对我来说,这比调用静态 String 函数要干净得多,同时仍然是 NullReference 安全的!

    【讨论】:

      【解决方案5】:

      我知道这是大约一年前发布的,但这是供用户将来参考。

      我遇到了类似的问题。就我而言(我会尽量简短,如果您想了解更多详细信息,请告诉我),我试图检查字符串是否为空(字符串是电子邮件的主题)。无论我做什么,它总是返回相同的错误消息。我知道我做对了,但它仍然不断抛出相同的错误信息。然后我突然意识到,我正在检查电子邮件(实例/对象)的主题(字符串)是否,如果电子邮件(实例)一开始就已经为空怎么办。我如何检查电子邮件的主题,如果电子邮件已经为空..我检查了电子邮件是否为空,它工作正常。

      在检查主题(字符串)时,我使用了 IsNullorWhiteSpace()、IsNullOrEmpty() 方法。

      if (email == null)
      {     
           break;    
      }
      else
      {    
           // your code here    
      }
      

      【讨论】:

      • 由于短路,你可以有类似'if(email == null || email.subject.IsNullOrWhiteSpace()){....'的东西,它应该处理空电子邮件部分就好了
      猜你喜欢
      • 1970-01-01
      • 2019-06-03
      • 2013-05-27
      • 2013-07-22
      相关资源
      最近更新 更多