gpt4 book ai didi

vb.net - 函数不会在所有代码路径上返回值。使用结果时,可能会在运行时发生空引用异常

转载 作者:行者123 更新时间:2023-12-04 13:40:12 25 4
gpt4 key购买 nike

我收到此错误:

Function 'getkey' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.



到以下代码:
 Public Function getkey(ByVal id As String)
Dim cmd As SqlCommand
Try
cmd = New SqlCommand("dbo.getkeydesc", GenLog.cn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@id", id)
Dim r As SqlDataReader = cmd.ExecuteReader()
If r.HasRows Then
Return True
Else
Return False
End If
Catch ex As Exception
MsgBox(ex.ToString)
Finally
' If Not cn Is Nothing Then cn.Close()
End Try
End Function

我尝试了所有可能的解决方案,但没有奏效。
任何帮助,将不胜感激。

最佳答案

Catch块不返回值。将其更改为返回值的位置,如下所示:

Public Function getkey(ByVal id As String)
Dim cmd As SqlCommand
Try
cmd = New SqlCommand("dbo.getkeydesc", GenLog.cn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@id", id)
Dim r As SqlDataReader = cmd.ExecuteReader()
If r.HasRows Then
Return True
Else
Return False
End If
Catch ex As Exception
MsgBox(ex.ToString)
Return False
Finally
' If Not cn Is Nothing Then cn.Close()
End Try
End Function

关于vb.net - 函数不会在所有代码路径上返回值。使用结果时,可能会在运行时发生空引用异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18721925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com