-1

Hello All I have a question, when try to add data in my class "response" with VB.Net and I run the debbug get a error Message "is detected Null ReferenceException" do you know as I should correct this error? could help me please. thanks.

Below part the code

Module Module1
 Sub Main()
  try
    dim res as new response

    a.customers(1).phoneNumber="11023321"
    a.customers(2).phoneNumbre="11023300"
    a.expand=False
    a.include=false


En sub


Public Class Response

    Public Property Customers As List(Of Customer)
    Public Property Expand As Boolean
    Public Property Include As Boolean

End Class

Public Class Customer

    Public Property PhoneNumber As String

End Class
Magnus
  • 45,362
  • 8
  • 80
  • 118
PJUAREZG
  • 1
  • 2

1 Answers1

0

It is probably that a.customers is never initialized, so it is null (by the way, that should be res.Customers, but I'll assume that's a typo). Even if it wasn't null, it would still be empty, so a.customers(1) would give you an ArgumentOutOfRangeException. You may want to start with the documentation for the List(Of T) class to see how to use it.

Something like this should be closer to what you are looking for:

Sub Main()

    Dim res As New Response()
    ' Populate customers with instances of the Customer class
    res.Customers.Add(New Customer() With { .PhoneNumber = "11023321" })
    res.Customers.Add(New Customer() With { .PhoneNumber = "11023300" })
    res.Expand = False
    res.Include = False

End Sub

Public Class Response

    ' Initialize the Customers list
    Public Property Customers As List(Of Customer) = New List(Of Customer)()
    Public Property Expand As Boolean
    Public Property Include As Boolean

End Class
Mark
  • 8,140
  • 1
  • 14
  • 29