user3363744
user3363744

Reputation: 169

VB adding to a list when initializing a constructor

I have been stuck for the past hour trying to initialize "one" card in my deck of cards. Everything works create but as soon as I try to add a Card to my deck I get a "null reference exception" error. I created a card with a value of 1 and 12 (suit, value) and try to add "the card object" to my list of cards. The values show up when I message box the information but my list will not take the card.

      Public Class deck
          'create the fields
          Private newDeck As List(Of Card)

          'create properties
          Property newDeck_Property As List(Of Card)
            Get
                Return newDeck
            End Get
            Set(value As List(Of Card))
                newDeck = value
            End Set
         End Property


      Sub New()
            Dim cardvalueinfo As CardValue
            cardvalueinfo.cSuite = 1
            cardvalueinfo.cValue = 12
            Dim newCardinsert As New Card(cardvalueinfo)
            MessageBox.Show(newCardinsert.oneCard_Prop.cSuite)
            MessageBox.Show(newCardinsert.oneCard_Prop.cValue)
            newDeck_Property.Add(newCardinsert) <--------------- null error here
      End Sub

  End Class

I would really appreciate anyone pointing me in the right direction. I am a noob

Thanks

Upvotes: 2

Views: 285

Answers (1)

har07
har07

Reputation: 89295

You need to initialize newDeck_Property before adding item to it :

newDeck_Property = New List(Of Card)

or put initialization along with declaration of the backing field :

Private newDeck As New List(Of Card)

Upvotes: 2

Related Questions