变量'reader'在被赋值之前使用。运行时可能会导致空引用异常

时间:2018-02-01 03:47:32

标签: vb.net

尝试修复警告并且不确定如何将代码重构为reader.IsClosed正在发出警告,指出“变量'阅读器'在被赋值之前使用。在运行时可能会导致空引用异常。 “从逻辑上讲,因为读者为SqlDataReader&&读者没有初始化一个值然后我可以忽略,因为在运行时应该没问题,但我的经验不足会让我相信有更好的方法吗?

Public Function GetTotalItems(ByVal userId As Long) As Int16

    Dim lstParam As List(Of SqlParameter) = New List(Of SqlParameter)()
    Dim tablMd = Me.GetMetaData()
    Dim retList As ArrayList = New ArrayList()

    lstParam.Add(New SqlClient.SqlParameter("@" + tablMd.PrimaryKey.ColumnName, 0))
    lstParam.Add(New SqlClient.SqlParameter("@UserID", userId))
    lstParam.Add(New SqlClient.SqlParameter("@ActionFlag", "SELECT_ITEMS_COUNT"))

    Dim spName As String = Me.GetStoreProcname()
    Dim reader As SqlDataReader
    Try
        reader = SqlHelper.ExecuteReader(
            Utility.GetConnectionStringSetting(),
            CommandType.StoredProcedure,
            Me.GetStoreProcname(),
            lstParam.ToArray()
        )

        If (reader.HasRows = True) Then
            If (reader.Read()) Then
                Dim value As Object = reader(0)

                Return CInt(value)

            End If

        End If

    Catch ex As Exception
        Throw

    Finally
        If Not reader.IsClosed Then
            reader.Close()
        End If
    End Try

    Return 0

End Function

2 个答案:

答案 0 :(得分:0)

@jmcilhinney,@ o_O,@ Chris Dunaway ...感谢您的帮助+欣赏+钦佩您的知识+敬畏== deverence(); ...这消除了错误:

Public Function GetTotalAmount(ByVal userId As Long)As Decimal

    Dim lstParam As List(Of SqlParameter) = New List(Of SqlParameter)()
    Dim tablMd = Me.GetMetaData()
    Dim retList As ArrayList = New ArrayList()

    lstParam.Add(New SqlClient.SqlParameter("@" + tablMd.PrimaryKey.ColumnName, 0))
    lstParam.Add(New SqlClient.SqlParameter("@UserID", userId))
    lstParam.Add(New SqlClient.SqlParameter("@ActionFlag", "SELECT_TOTAL_AMOUNT"))

    Dim spName As String = Me.GetStoreProcname()


    Using reader As SqlDataReader = SqlHelper.ExecuteReader(
            Utility.GetConnectionStringSetting(),
            CommandType.StoredProcedure,
            Me.GetStoreProcname(),
            lstParam.ToArray()
        )

        If (reader.HasRows = True) Then
                If (reader.Read()) Then
                    Dim value As Object = reader(0)

                    Return CDec(value)
                End If
            End If
    End Using

    Return 0

End Function

答案 1 :(得分:0)

我们可以将问题缩小到这个摘录:

Dim reader As SqlDataReader
Try
    reader = SqlHelper.ExecuteReader( ... )
Finally
    If Not reader.IsClosed Then reader.Close()
End Try

如果ExecuteReader()函数抛出异常,则会出现问题。在那种情况下,永远不会为reader变量赋值。当您尝试评估Nothing时,它仍为reader.IsClosed,而

鉴于您实际上没有对异常做任何事情而且SqlHelper负责连接和命令对象,您可以将整个功能缩小到这样:

Public Function GetTotalItems(ByVal userId As Long) As Int16
    Dim lstParam = {
       New SqlClient.SqlParameter("@" + Me.GetMetaData().PrimaryKey.ColumnName, 0),
       New SqlClient.SqlParameter("@UserID", userId),
       New SqlClient.SqlParameter("@ActionFlag", "SELECT_ITEMS_COUNT")
    }

    Using reader As SqlDataReader = SqlHelper.ExecuteReader(
            Utility.GetConnectionStringSetting(),
            CommandType.StoredProcedure,
            Me.GetStoreProcname(),
            lstParam)

        If reader.Read() Then Return CInt(reader(0))
    End Using

    Return 0  
End Function