Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2007

Tip: Looking for answers? Try searching our database.

Object reference not set to an instance of an object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sam - 08 Jun 2007 19:38 GMT
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
Jon Skeet [C# MVP] - 08 Jun 2007 21:50 GMT
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.