Function doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.
-
Hi G to URU's I have a function which returns data table, amongst other functions I created this one returns a warning: Warning 4 Function 'getSectionContent' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used. Please Advice. Thanks Dom Here's the function Private Function getSectionContent() As DataTable Dim con As New cls_Connection(enu_Databases.Customers) Dim clsSelect As cmp_spSelectSectionContent = Nothing _dt = New DataTable Try con.BeginTransaction() clsSelect = New cmp_spSelectSectionContent(con.SQLConnection, con.SQLTransaction) If Not clsSelect.fill(_courseSectionID, _dt) = True Then Throw New Exception(clsSelect.ErrorMessage) Else con.CommitTransaction() Return _dt End If Catch ex As Exception con.RollbackTransaction() Finally con.Dispose() End Try End Function
-
Hi G to URU's I have a function which returns data table, amongst other functions I created this one returns a warning: Warning 4 Function 'getSectionContent' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used. Please Advice. Thanks Dom Here's the function Private Function getSectionContent() As DataTable Dim con As New cls_Connection(enu_Databases.Customers) Dim clsSelect As cmp_spSelectSectionContent = Nothing _dt = New DataTable Try con.BeginTransaction() clsSelect = New cmp_spSelectSectionContent(con.SQLConnection, con.SQLTransaction) If Not clsSelect.fill(_courseSectionID, _dt) = True Then Throw New Exception(clsSelect.ErrorMessage) Else con.CommitTransaction() Return _dt End If Catch ex As Exception con.RollbackTransaction() Finally con.Dispose() End Try End Function
Its pretty self-explanatory really, not all code paths inside that function will return a value. Your return statement is inside a try block, if that block throws an exception the return statement will never be reached. Its generally bad practice to put return statements half way through methods anyway, can cause maintenance nightmares. Also I don't think your finally code will ever run unless an exception is thrown. Put the return statement right at the end of the function.
-
Its pretty self-explanatory really, not all code paths inside that function will return a value. Your return statement is inside a try block, if that block throws an exception the return statement will never be reached. Its generally bad practice to put return statements half way through methods anyway, can cause maintenance nightmares. Also I don't think your finally code will ever run unless an exception is thrown. Put the return statement right at the end of the function.
Thanks badgrs, I think I got your point, since its a function I still need to return something even when it hit exemption, I placed return nothing on catch statement, this way I could test if my datatable contains something or nothing at all. Thanks for the help Dom Updated snips: Private Function getSectionContent(Optional ByVal sectionContentID As Integer = 0) As DataTable Dim con As New cls_Connection(enu_Databases.Customers) Dim clsSelect As cmp_spSelectSectionContent = Nothing _dt = New DataTable Try con.BeginTransaction() clsSelect = New cmp_spSelectSectionContent(con.SQLConnection, con.SQLTransaction) If Not clsSelect.fill(_courseSectionID, _dt) = True Then Throw New Exception(clsSelect.ErrorMessage) Else con.CommitTransaction() Return _dt End If Catch ex As Exception con.RollbackTransaction() Return Nothing Finally con.Dispose() End Try End Function
-
Thanks badgrs, I think I got your point, since its a function I still need to return something even when it hit exemption, I placed return nothing on catch statement, this way I could test if my datatable contains something or nothing at all. Thanks for the help Dom Updated snips: Private Function getSectionContent(Optional ByVal sectionContentID As Integer = 0) As DataTable Dim con As New cls_Connection(enu_Databases.Customers) Dim clsSelect As cmp_spSelectSectionContent = Nothing _dt = New DataTable Try con.BeginTransaction() clsSelect = New cmp_spSelectSectionContent(con.SQLConnection, con.SQLTransaction) If Not clsSelect.fill(_courseSectionID, _dt) = True Then Throw New Exception(clsSelect.ErrorMessage) Else con.CommitTransaction() Return _dt End If Catch ex As Exception con.RollbackTransaction() Return Nothing Finally con.Dispose() End Try End Function
-
Its pretty self-explanatory really, not all code paths inside that function will return a value. Your return statement is inside a try block, if that block throws an exception the return statement will never be reached. Its generally bad practice to put return statements half way through methods anyway, can cause maintenance nightmares. Also I don't think your finally code will ever run unless an exception is thrown. Put the return statement right at the end of the function.