0

I have a N-Tier ASP.NET 4.8 WebForms app.

I am attempting to: 1) Call a function at the Business Layer, which will then 2) Call a function at the Data Layer which will then 3) Query the database results into a DataTable and then 4) Return that DataTable to the Business Layer and then 5) return the same DataTable to the Presentation Layer.

When I call the Business Layer function directly from the Presentation Layer, I get a Null Reference Error "Object reference not set to an instance of an object."

This exception occurs at the Business Layer "Return RewardsDataAccessContext.GetRewardTypes()"

Presentation Layer

Dim RewardsContext As New Library.Rewards
dgTest.DataSource = RewardsContext.GetRewardTypes()
dgTest.DataBind()

Business Layer

Public Class Rewards
    Public Function GetRewardTypes() As DataTable
        Dim RewardsDataAccessContext As DataAccess.Rewards
        Return RewardsDataAccessContext.GetRewardTypes()
    End Function
End Class

Data Layer

 Protected dtReturn As DataTable = New DataTable()

    Public Function GetRewardTypes() As DataTable
        Using conn = New SqlConnection(ConfigurationManager.ConnectionStrings("csName").ConnectionString)
            Using cmd = New SqlCommand("spName", conn)
                cmd.CommandType = CommandType.StoredProcedure
                conn.Open()

                dtReturn.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection))
                Return dtReturn
            End Using
        End Using
    End Function

If I call the Data Layer function directly from the Presentation Layer, I get the expect DataTable returned in-tact. Only when it passes through the Business Layer (Library) do I get an exception.

Dim RewardsDataAccessContext As New DataAccess.Rewards
dgTest.DataSource = RewardsDataAccessContext.GetRewardTypes()
dgTest.DataBind()
Ryan.B
  • 30
  • 7
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – J... Mar 24 '22 at 18:30
  • I noticed that prior. It did not help. My confusion lies in the fact that the request works if I connect the Presentation Layer directly to the Data Layer, but fails when I put the Business Layer in the middle. All the BL does is Return the call to the DL. – Ryan.B Mar 24 '22 at 18:33
  • I added to the question. The exception [NullReferenceException: Object reference not set to an instance of an object.] occurs in the Business Layer, at "Return RewardsDataAccessContext.GetRewardTypes()" – Ryan.B Mar 24 '22 at 18:39
  • 1
    Thank you very much for that suggestion. I had not properly created that instance, and that is why that particular call failed, while others worked. For whatever reason, I did not notice the omission. Very grateful for your help. – Ryan.B Mar 24 '22 at 18:45

1 Answers1

-1

We can't see the definition of DataAccess.Rewards, but we can see that GetRewardTypes() is not Shared and you've declared

Dim RewardsDataAccessContext As DataAccess.Rewards

so assuming that this is a class you have to create an instance before using it. Did you mean

Dim RewardsDataAccessContext As DataAccess.Rewards = New DataAccess.Rewards()

perhaps?

J...
  • 30,968
  • 6
  • 66
  • 143