Please I am trying to understand the arraylist.
Please anyone help; why I am getting this message "Object reference not set
to an instance of an object" when line no (13) get executed in the following
code:
1- ' Create a new ArrayList to hold the Customer objects.
2- Dim customerArray As New ArrayList()
3- 'Add customers to the ArrayList of Customer objects.
4- Dim x As Integer
5- For x = 0 To 999
6- customerArray.Add(New Customer("Customer" + x.ToString()))
7- Next x
8- ' Add orders to each Customer object in the ArrayList.
9- Dim customer1 As Customer
10- For Each customer1 In customerArray
11- Dim y As Integer
12- For y = 0 To 14
13- customer1.CustomerOrders.Add(New Order("Order" +
y.ToString()))
14- Next y
-----------Customer & Order Classes-----
Public Class Customer
Private _CustomerOrders As ArrayList
Private _Name As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Property CustomerOrders() As ArrayList
Get
Return _CustomerOrders
End Get
Set(ByVal value As ArrayList)
_CustomerOrders = value
End Set
End Property
Public Sub New(ByVal Name As String)
_Name = Name
End Sub
End Class
---------------
Public Class Order
Private _Name As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Sub New(ByVal Name As String)
_Name = Name
End Sub
End Class
------------------
Thank you
> Please I am trying to understand the arraylist.
>
> Please anyone help; why I am getting this message "Object reference not set
> to an instance of an object" when line no (13) get executed in the following
> code:
I can't see anything in your code which initializes customer1.
_CustomerOrders, which would certainly explain the exception. You need
to initialize the underlying ArrayList somewhere.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sam - 08 Jun 2007 22:05 GMT
Thank you Sir.
I added the line no 4 and it works.
1 ' Add orders to each Customer object in the ArrayList.
2 Dim customer1 As Customer
3 For Each customer1 In customerArray
4 customer1.CustomerOrders = New ArrayList
5 Dim y As Integer
6 For y = 0 To 14
7 customer1.CustomerOrders.Add(New Order("Order" +
y.ToString()))
8 Next y
9 Next customer1
Thank you again.
> > Please I am trying to understand the arraylist.
> >
[quoted text clipped - 5 lines]
> _CustomerOrders, which would certainly explain the exception. You need
> to initialize the underlying ArrayList somewhere.