?
Solved

NullReferenceException when adding an item to an empty list

Posted on 2009-04-27
3
Medium Priority
?
338 Views
Last Modified: 2012-06-21
I'm having some trouble understanding the correct way to initialize lists and add to them.  See my code below - I have a Customer class which I initialize without knowing the addresses, and then I want to add addresses to the customer object afterwards.  However, I get a NullReferenceException whenever I try to call the AddAddress method.  I'm sure I'm probably missing some kind of initialization somewhere (when I step through in the debugger, the _CustProfileAddresses list is set to Nothing when I try to add the new address), but I can't figure out the right way to do it.

Thanks!
Public Class Customer
        Private _CustomerID As Int32
        Private _CustProfile As CustProfile
        Private _CustProfileAddresses As List(Of CustProfileAddress)
        Public ReadOnly Property CustID() As Int32
            Get
                Return _CustID
            End Get
        End Property
        Public ReadOnly Property CustProfile() As CustProfile
            Get
                Return _CustProfile
            End Get
        End Property
        Public ReadOnly Property CustProfileAddresses() As List(Of CustProfileAddress)
            Get
                Return _CustProfileAddresses
            End Get
        End Property
 
        Public Sub New(ByVal CustID As Int32, ByVal ProfileMain As CustProfile, Optional ByVal Addresses As List(Of CustProfileAddress) = Nothing)
            _CustID = CustID
            _CustProfile = CustMain
            _CustProfileAddresses = Addresses
        End Sub
 
        Public Sub AddAddress(ByVal Address As CustProfileAddress)
            _CustProfileAddresses.Add(Address)
        End Sub
 
 
'And the calling class
        Dim NewCustMain As New CustProfile(1, "", "") 'params here
 
        Dim CustSearchResult As New Customer(1, NewCustMain)
 
        CustSearchResult.AddAddress(New CustProfileAddress("test", "", "") 'params here
        'NullReferenceException thrown

Open in new window

0
Comment
Question by:Jayrw
  • 2
3 Comments
 
LVL 48

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 2000 total points
ID: 24245605
Change the New routine to this....
        Public Sub New(ByVal CustID As Int32, ByVal ProfileMain As CustProfile, Optional ByVal Addresses As List(Of CustProfileAddress) = Nothing)
            _CustID = CustID
            _CustProfile = CustMain
            If Addresses Is Nothing Then
                _CustProfileAddresses = New List(Of CustProfileAddress)
            Else
                _CustProfileAddresses = Addresses
            End If
        End Sub

Open in new window

0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 24245637
I just noticed something unrelated to this problem. It appears 'CustMain' should be 'ProfileMain'...
        Public Sub New(ByVal CustID As Int32, ByVal ProfileMain As CustProfile, Optional ByVal Addresses As List(Of CustProfileAddress) = Nothing)
            _CustID = CustID
            _CustProfile = ProfileMain
            If Addresses Is Nothing Then
                _CustProfileAddresses = New List(Of CustProfileAddress)
            Else
                _CustProfileAddresses = Addresses
            End If
        End Sub

Open in new window

0
 

Author Closing Comment

by:Jayrw
ID: 31575176
Excellent - thanks for your help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question