title: Object reference not set to an instance of an object - Black Jack Game
body: <p>I'm a bit of a programming newbie and I'm making a Black Jack game, but I'm really struggling to add a card from my shuffled deck, into the player's hand, when I called the <code>Hit</code> function I get this message: </p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <p>I know the solution is probably really obvious but could you please help?<br>
  The problem is right at the bottom of the <code>Deck</code> Class, in the <code>Hit()</code> Function</p>
  
  <p>/* Main Class */</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  
  namespace BlackJackGameX
  {
      public class MainClass
      {
          public static void Main (string[] args)
          {
  
              Deck cards = new Deck();
  
              Hand playerHand = new Hand(cards);
  
              Console.WriteLine("Welcome to Black Jack\n\nPress Enter To Start");
              Console.ReadLine ();
  
              cards.Hit();
  
              playerHand.PrintHand();
  
          }
      }
  }
  </code></pre>
  
  <p>/* Card Class */</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  
  namespace BlackJackGameX
  {
      public enum Suit {Spades, Hearts, Clubs, Diamonds}
      public enum FaceValue {Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King}
  
      public class Card
      {
          public Suit CardSuit;
          public FaceValue CardValue;
          public int iValue;
  
          public Card (Suit cardSuit, FaceValue cardValue, int ivalue)
          {
              CardSuit = cardSuit;
              CardValue = cardValue;
              iValue = ivalue;
          }
  
          public Card ()
          {
  
          }
      }
  }
  </code></pre>
  
  <p>/* Hand Class */</p>
  
  <pre><code>using System.Collections.Generic;
  using System.Text;
  
  namespace BlackJackGameX
  {
      public class Hand
      {
          public List&lt;Card&gt; PlayerHand;
  
          private Deck cardDeck = new Deck();
  
          public Hand (Deck Cards)
          {
              cardDeck = Cards;
          }
  
          public void PrintHand()
          {
              for (int i = 0; i &lt; PlayerHand.Count; ++i)
              {
                  Console.WriteLine("You have a " + PlayerHand[i].CardValue + " of " + PlayerHand[i].CardSuit);
  
                  if (i &lt; PlayerHand.Count)
                  {
                      Console.WriteLine ("&amp;");
                  }
              }
  
              Console.ReadLine();
  
          }
      }
  }
  </code></pre>
  
  <p>/* Deck Class */</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  
  namespace BlackJackGameX
  {
      public class Deck
      {
          Hand playerHand;
  
          Random rNumber = new Random();
  
          List&lt;Card&gt; Cards;
  
          public int iValue1 = 11;
          public int iValue2 = 2;
          public int iValue3 = 3;
          public int iValue4 = 4;
          public int iValue5 = 5;
          public int iValue6 = 6;
          public int iValue7 = 7;
          public int iValue8 = 8;
          public int iValue9 = 9;
          public int iValue10 = 10;
          public int iValue11 = 10;
          public int iValue12 = 10;
          public int iValue13 = 10;
  
          public Deck()
          {
              Cards = NewDeck();
          }
  
          private List&lt;Card&gt; NewDeck()
          {
              var AllSuits = new Suit[]
              {
                  Suit.Spades,
                  Suit.Hearts,
                  Suit.Clubs,
                  Suit.Diamonds
              };
  
              var AllFaces = new FaceValue[]
              {
                  FaceValue.Ace,
                  FaceValue.Two,
                  FaceValue.Three,
                  FaceValue.Four,
                  FaceValue.Five,
                  FaceValue.Six,
                  FaceValue.Seven,
                  FaceValue.Eight,
                  FaceValue.Nine,
                  FaceValue.Ten,
                  FaceValue.Jack,
                  FaceValue.Queen,
                  FaceValue.King
              };
  
              var AllValues = new int[]
              {
                  iValue1,
                  iValue2,
                  iValue3,
                  iValue4,
                  iValue5,
                  iValue6,
                  iValue7,
                  iValue8,
                  iValue9,
                  iValue10,
                  iValue11,
                  iValue12,
                  iValue13
              };
  
              List&lt;Card&gt; myDeck = new List&lt;Card&gt;();
  
              for (int i = 0; i &lt;= 3; i++) 
              {
                  for (int j = 0; j &lt;= 12; j++) 
                  {
                      Card newCard = new Card(AllSuits[i], AllFaces[j], AllValues[j]);
                      myDeck.Add(newCard);
  
                  }
              }
  
              List&lt;Card&gt; shuffledDeck = new List&lt;Card&gt; ();
  
              while (myDeck.Count &gt; 0)
              {
                  int c = myDeck.Count;
  
                  int n = rNumber.Next (0, c);
  
                  // var will translate into an int which will be used to pick the card needed from the list
                  var value = myDeck[n];
                  shuffledDeck.Add(value);
                  myDeck.Remove(value);
  
              }
  
              return shuffledDeck;
          }
  
          public void Print()
          {
              for (int i = 0; i &lt;= 51; i++) 
              {
                  Console.WriteLine("You drew a " + (Cards [i].CardValue) + " of " + (Cards [i].CardSuit));
                  Console.ReadLine ();
              }
          }
  
          public void Hit()
          {
  
              playerHand.PlayerHand.Add(Cards[0]);
              Cards.Remove(Cards[0]);
  
              /*var tmp = Cards[0];
              Cards.Remove(tmp);
              return tmp;*/
          }
  
      }
  }
  </code></pre>