DataReader within try block causing potential null reference error

exception, vb.net, visual-studio

Solution

Explicitly initialize the dr declaration to Nothing as such:

Dim dr As DbDataReader = Nothing

And the warning will disappear.

Problem

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 ``` 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.

Original source