I've got the following bit of code (that another developer wrote) that I need to modify because the ExecuteProc procedure fails. (The reason why it fails isn't in the scope of this question. It's a long story) but in scope is that I'm not sure why a NullReferenceException
would occur. It's in VB.NET (I'm not a VB.NET guy myself, so I'm not sure if this is something that is unique to VB.NET):
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports System.Collections
'(...)
Public Overloads Function ExecuteProc( ByVal pvsProcName as String) As DataSet
Dim oCommand As SqlCommand
Dim oDataAdaptor As SqlDataAdapter = New SqlDataAdapter
'Other Stuff here
Try
oCommand = CommandGet(ConnectionGet)
'Do some more stuff that's irrelevant
Catch oException As Exception
Throw oException
Finally
ConnectionClose(oCommand.Connection)
oCommand.Dispose()
oDataAdaptor.Dispose()
End Try
End Function
I get the following warning:
Variable 'oCommand' is used before it has been assigned a value. A null reference exception could result at runtime.`
Edit: I found the answer right before I posted the question. I posted it to keep it around (I hate putting ten minutes into something to dump it willy nilly).
My subsequent question for VB.NET folks is this:
What would be the difference in the following two intializations:
Dim oCommand As SqlCommand = Nothing
And:
Dim oCommand as New SqlComand