question

nachoshaw-9496 avatar image
0 Votes"
nachoshaw-9496 asked nachoshaw-9496 answered

Visual studio 2017 vs 2019 - SysatemNullReferenceException

Hi

When i run my project in 2017, it works just fine but when i run it in 2019, i get a System.NullReferenceException. Here is my function

 Public Function GetConnection() As SqlConnection
         Dim Connection As SqlConnection = Nothing
         Dim bNeedAdd As Boolean = False
         Try
             Connection = CType(_Connections(ConnString), SqlConnection)
         Catch ex As Exception
         End Try
         If Connection Is Nothing Then
             bNeedAdd = True
         End If
         If Connection Is Nothing OrElse Connection.State = ConnectionState.Broken OrElse Connection.State = ConnectionState.Closed Then
             Try
                 Connection.Dispose() <--- Error line in 2019
                 Connection = Nothing
             Catch ex As Exception
                 Debug.Print("Connection already exists")
             End Try
             Connection = New SqlConnection
         End If
    
         'Always return an open connection
         If Connection.State = ConnectionState.Closed Then
             Connection.ConnectionString = ConnString
             ' Connection.Open()
         End If
         If bNeedAdd Then
             _Connections.Add(ConnString, Connection)
         End If
         Return Connection
     End Function

Seeing as this line is inside a Try Catch, I am wondering why its throwing this error. Is there a setting or a reason why this behaviour occurs?


Thanks

dotnet-visual-basicdotnet-runtime
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered

Hi,
if Connection Is Nothing you get NullReferenceException:

  1. Set Connection to Nothing

       Dim Connection As SqlConnection = Nothing
          Dim bNeedAdd As Boolean = False
          Try
              Connection = CType(_Connections(ConnString), SqlConnection)
          Catch ex As Exception
    
  2. Ignore Error if there isn't Connection in _Connections

       End Try
    
          If Connection Is Nothing Then
              bNeedAdd = True
          End If
    
          If `Connection Is Nothing` OrElse ... Then
              Try
                  Connection.Dispose() <--- Error line in 2019
    

Change your code:

      If `Connection Is Nothing` OrElse ... Then
          Try
              Connection?.Dispose()



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

nachoshaw-9496 avatar image
0 Votes"
nachoshaw-9496 answered

Thanks for the reply. The question mark (null Conditional Operator) makes all the difference :)



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.