为什么我无法检查连接字符串上的空引用?
可能是一个 C# 菜鸟问题,所以不要攻击我。 我试图这样做:
if (ConfigurationManager.ConnectionStrings["PrimaryConnectionString"].ConnectionString != null)
{
// ...
}
但我不断收到System.NullReferenceException
。 我想既然它返回一个字符串,我就可以检查 null
并继续。 我花了一段时间才找到正确的方法:
ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["PrimaryConnectionString"];
if (cs != null)
{
this.Connection.ConnectionString = cs.ConnectionString;
}
因此在其他情况下,例如检查 Session
对象的某些值,我会像这样检查 null
:
if (Session["EmployeeID"] != null)
{
_EmployeeID = System.Convert.ToInt32(Session["EmployeeID"]);
}
所以我只是想知道你怎么知道什么时候可以或不能检查 null
?
Probably a C# noob question, so don't flame me. I was trying to do this:
if (ConfigurationManager.ConnectionStrings["PrimaryConnectionString"].ConnectionString != null)
{
// ...
}
But I kept getting a System.NullReferenceException
. I thought since it returns a string that I could just check for null
and move on. It took me a while to find the right way to do it:
ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["PrimaryConnectionString"];
if (cs != null)
{
this.Connection.ConnectionString = cs.ConnectionString;
}
So in other instances, like checking a Session
object for some value I would do a check for null
like this:
if (Session["EmployeeID"] != null)
{
_EmployeeID = System.Convert.ToInt32(Session["EmployeeID"]);
}
So I just wanted to know how do you know when you can or can't do a check for null
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以随时检查是否为 null(不可为 null 的值类型除外,该类型不能为 null)。
当您希望访问对象的成员时,您应该检查是否为 null,并且您不确定包含该对象的变量是否不为 null。
空检查的几个典型位置是:
You can check for null at any time you like (except with non-nullable value types, that cannot be null).
You should check for null at any time you wish to access the members of an object, and you are not certain that the variable containing the object is not null.
A couple of typical places for null check are:
NullReferenceException
if the parameter is null. Do a null check on the variable you wish to pass before calling the method.您的第一个测试检查 ConnectionStringSettings 中的连接字符串是否为 null。 第二个测试检查 ConnectionStringSettings 引用本身是否为 null。 这是之间的区别:
和
如果
person
为 null,则第一个将会爆炸; 第二个不会发现person.Name
是否为 null。 如果你想检查两者,你需要:某些语言(例如 Groovy)有一个 null-safe 解引用运算符,所以你可以这样做:
如果你想测试是否任何 可能很长的表达式的一部分为 null。 不幸的是 C# 没有这个:(
Your first test checks whether the connection string within the ConnectionStringSettings is null. The second test checks whether the ConnectionStringSettings reference itself is null. It's the difference between:
and
The first will go bang if
person
is null; the second won't spot ifperson.Name
is null. If you want to check both, you need:Some languages (such as Groovy) have a null-safe dereferencing operator, so you can do:
That makes this sort of test easier, if you want to test whether any part of a potentially long expression is null. Unfortunately C# doesn't have that :(
您的空引用异常发生在父对象 (ConfigurationManager.ConnectionStrings["PrimaryConnectionString"]) 中。 一旦你也检查了,你就没事了。
试试这个:
请注意,如果您使用其中的更多方法,则将这些检查分开会很有用。
Your null reference exception happened in parent object (ConfigurationManager.ConnectionStrings["PrimaryConnectionString"]). Once you check it also, you are fine.
try this:
Notice that if you use more methods from it, it would be useful to separate these checks.
您的问题是您正在检查:
是否有空指针。
事实上,
为 null,因此,当您尝试取消引用 that 来获取连接字符串时,就会出现异常。 实际上,您正在做的是:
这是有问题的。
我倾向于避免在单个语句中进行多层取消引用,或者在整个事情周围放置一个异常处理程序以在任何时候捕获问题。
Your problem was that you were checking:
for a null pointer.
In actual fact,
was null so that, when you tried to dereference that to get the connection string, that's when you got the exception. Effectively, what you're doing is:
which is problematic.
I tend to either avoid many layers of dereferencing in a single statement or place an exception handler around the whole thing to catch problems at any point.