There is probably is simple fix for this but I currently have code similar to
可能有一个简单的解决方案,但我目前有类似的代码
dim dr as dbDataReader
try
dr = connection.getDataReader(sql_str)
Catch ex as sqlClientException
log.error(ex)
finally
if not IsNothing(dr) then
dr.close
end if
end try
However Visual Studio still warns me that the
但是Visual Studio仍然警告我
if not IsNothing(dr) then
dr.close
end if
Can cause a NullReferenceException. What is the best way to mitigate this? I can't move the declaration into the try block.
可能导致NullReferenceException。减轻这种情况的最佳方法是什么?我无法将声明移到try块中。
4 个解决方案
#1
9
Explicitly initialize the dr declaration to Nothing as such:
将dr声明显式初始化为Nothing:
Dim dr As DbDataReader = Nothing
And the warning will disappear.
警告将消失。
#2
4
If GetDataReader
throws, dr
will not be assigned and will still have its initial value. VB.NET does initialise references to Nothing
, but the compiler will not let you rely on that.
如果GetDataReader抛出,则不会分配dr并且仍将具有其初始值。 VB.NET会初始化对Nothing的引用,但编译器不会让你依赖它。
If using .NET 2.0, I would recommend a Using
statement:
如果使用.NET 2.0,我建议使用Using语句:
Using dr As DbDataReader = connection.GetDataReader(sql_str)
Try
' Do something with dr '
Catch ex As SqlClientException
log.error(ex)
End Try
End Using
#3
0
Your code is correct. In the finally
statement, as long as you check to make sure your objects are not null, it won't throw a null reference exception based on what happens in your code.
你的代码是正确的。在finally语句中,只要检查以确保对象不为null,它就不会根据代码中发生的情况抛出空引用异常。
#4
0
I don't remember exactly, how VB.NET initialize variable, but in C# you must set "dr" some value/reference before you use them somewhere.
我不记得究竟,VB.NET如何初始化变量,但在C#中,你必须在某些地方使用它们之前设置“dr”一些值/引用。
I think this should work:
我认为这应该有效:
dim dr as dbDataReader
try
dr = connection.getDataReader(sql_str)
Catch ex as sqlClientException
log.error(ex)
dr = Nothing ' there is the change '
' finaly block is not necesary '
end try
if not IsNothing(dr) then
dr.close
end if
#1
9
Explicitly initialize the dr declaration to Nothing as such:
将dr声明显式初始化为Nothing:
Dim dr As DbDataReader = Nothing
And the warning will disappear.
警告将消失。
#2
4
If GetDataReader
throws, dr
will not be assigned and will still have its initial value. VB.NET does initialise references to Nothing
, but the compiler will not let you rely on that.
如果GetDataReader抛出,则不会分配dr并且仍将具有其初始值。 VB.NET会初始化对Nothing的引用,但编译器不会让你依赖它。
If using .NET 2.0, I would recommend a Using
statement:
如果使用.NET 2.0,我建议使用Using语句:
Using dr As DbDataReader = connection.GetDataReader(sql_str)
Try
' Do something with dr '
Catch ex As SqlClientException
log.error(ex)
End Try
End Using
#3
0
Your code is correct. In the finally
statement, as long as you check to make sure your objects are not null, it won't throw a null reference exception based on what happens in your code.
你的代码是正确的。在finally语句中,只要检查以确保对象不为null,它就不会根据代码中发生的情况抛出空引用异常。
#4
0
I don't remember exactly, how VB.NET initialize variable, but in C# you must set "dr" some value/reference before you use them somewhere.
我不记得究竟,VB.NET如何初始化变量,但在C#中,你必须在某些地方使用它们之前设置“dr”一些值/引用。
I think this should work:
我认为这应该有效:
dim dr as dbDataReader
try
dr = connection.getDataReader(sql_str)
Catch ex as sqlClientException
log.error(ex)
dr = Nothing ' there is the change '
' finaly block is not necesary '
end try
if not IsNothing(dr) then
dr.close
end if