你调用的对象是空的。 [英] Object reference not set to an instance of an object.

查看:271
本文介绍了你调用的对象是空的。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直当我运行该程序收到此错误。


  

对象引用未设置到对象的实例。
  说明:执行当前Web请求的执行过程中发生未处理的异常。
  请查看有关错误的详细信息的堆栈跟踪以及它起源于code。
  异常详细信息:System.NullReferenceException:未将对象引用设置到对象的实例


 源错误:符合错误:156线:如果(strSearch ==|| strSearch.Trim()长度== 0)

这是什么应该写入正确的方法是什么?


解决方案

在.NET 4.0中正确的方法是:

 如果(String.IsNullOrWhiteSpace(strSearch))

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

 如果(strSearch == NULL || strSearch == ||的String.Empty strSearch.Trim()。长度== 0)
//的String.Empty是一样的,


  

参考IsNullOrWhiteSpace方法


  
  

<一个href=\"http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx\">http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx


  
  

指示指定字符串是否为Nothing,空,或由
  只有空白字符。


在早期版本中,你可以做这样的事情:

 如果(String.IsNullOrEmpty(strSearch)|| strSearch.Trim()。长度== 0)

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

 如果(strSearch == NULL || strSearch ==的String.Empty)

这意味着你还需要检查你的IsWhiteSpace情况下, .Trim()。长度== 0 按例子。


  

参考IsNullOrEmpty方法


  
  

<一个href=\"http://msdn.microsoft.com/en-us/library/system.string.isnullorempty.aspx\">http://msdn.microsoft.com/en-us/library/system.string.isnullorempty.aspx


  
  

指示指定的字符串是Nothing或空字符串。


说明:

您需要确保 strSearch (或任何与此有关的变量)不是取消引用它使用前点字符( ) - 即你之前做 strSearch.SomeMethod() strSearch .SomeProperty 您需要检查 strSearch!= NULL

在你的榜样,你要确保你的字符串有一个值,这意味着你要确保字符串:


  • 不为空

  • 是不是空字符串(的String.Empty /

  • 不只是空格

在上述情况下,必须把它是空?第一次的情况下,所以也没有去检查其他情况下(和错误)时,该字符串为

I keep getting this error when I run the program.

Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line with error:

Line 156:        if (strSearch == "" || strSearch.Trim().Length == 0)

What is the correct way it should be written?

解决方案

The correct way in .NET 4.0 is:

if (String.IsNullOrWhiteSpace(strSearch))

The String.IsNullOrWhiteSpace method used above is equivalent to:

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

Reference for IsNullOrWhiteSpace method

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

Indicates whether a specified string is Nothing, empty, or consists only of white-space characters.

In earlier versions, you could do something like this:

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

The String.IsNullOrEmpty method used above is equivalent to:

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

Which means you still need to check for your "IsWhiteSpace" case with the .Trim().Length == 0 as per the example.

Reference for IsNullOrEmpty method

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

Indicates whether the specified string is Nothing or an Empty string.

Explanation:

You need to ensure strSearch (or any variable for that matter) is not null before you dereference it using the dot character (.) - i.e. before you do strSearch.SomeMethod() or strSearch.SomeProperty you need to check that strSearch != null.

In your example you want to make sure your string has a value, which means you want to ensure the string:

  • Is not null
  • Is not the empty string (String.Empty / "")
  • Is not just whitespace

In the cases above, you must put the "Is it null?" case first, so it doesn't go on to check the other cases (and error) when the string is null.

这篇关于你调用的对象是空的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆