Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Function doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.

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

Scheduled Pinned Locked Moved ASP.NET
html
5 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    firestoper
    wrote on last edited by
    #1

    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

    B 1 Reply Last reply
    0
    • F firestoper

      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

      B Offline
      B Offline
      badgrs
      wrote on last edited by
      #2

      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.

      F G 2 Replies Last reply
      0
      • B badgrs

        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.

        F Offline
        F Offline
        firestoper
        wrote on last edited by
        #3

        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

        B 1 Reply Last reply
        0
        • F firestoper

          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

          B Offline
          B Offline
          badgrs
          wrote on last edited by
          #4

          Why not just add the Return _dt to the end of the function, so if theres an exception you'll return an empty datatable rather than null. That would seem more logical to me.

          1 Reply Last reply
          0
          • B badgrs

            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.

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            badgrs wrote:

            Also I don't think your finally code will ever run unless an exception is thrown.

            The code in the Finaly block will always run.

            --- single minded; short sighted; long gone;

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups