0

I am working on developing a playing cards game in C# as part of my training in this new language. I have received several errors that I have been able to Google Fu and work out on my own, but this current error of:

Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
at Deck..ctor () [0x00087] in :0
at Program.Main (System.String[] args) [0x00000] in :0
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
at Deck..ctor () [0x00087] in :0
at Program.Main (System.String[] args) [0x00000] in :0

has me blocked. I have tried a few different ways of refactoring public class Card and the Card constructor function, but I just get different errors and more errors and don't get any closer to resolving it as what the error message above is telling me is that I haven't correctly initialized the object Card card = new Card(rank, suit);

  using System.Collections.Generic;

  public class Deck {
      // public List<Card> Cards = new List<Card>();
      public List<Card> cards;

  public Deck() {
      string[] ranks = { "Ace", "Two", "Three", "Four", "Five" };
      string[] suits = { "Diamonds", "Hearts", "Clubs", "Spades" };

      foreach (string suit in suits) {
          foreach (string rank in ranks) {
              Card card = new Card(rank, suit);
              cards.add(card);
          }
      }
  }
}

public class Card {
    // properties
    public string suit { get; set; }
    public string rank { get; set; }

    public Card(string rank, string suit){
       //initializations
       rank = rank;
       suit = suit;
    }
}

I looked through similar posts, but they are not specific to my case and I have also been following this guide:

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-initialize-objects-by-using-an-object-initializer#example

Daniel
  • 14,004
  • 16
  • 96
  • 156
  • 3
    Use the debugger to figure out what is `null` (probably `cards`), then make it not null. – SLaks Jun 13 '19 at 17:03
  • 4
    You have the correct code in place, commented out. – Blorgbeard Jun 13 '19 at 17:03
  • 1
    Additionally, it should be `this.suit = suit;` in the `Card` constructor, and same for rank. – Blorgbeard Jun 13 '19 at 17:04
  • 1
    Change `public List cards;` to `public List cards = new List();` – soccer7 Jun 13 '19 at 17:06
  • Thanks, this helps, I still get `error CS0103: The name cards does not exist in the current context` – Daniel Jun 13 '19 at 17:11
  • 1
    @Daniel hey List has "Add" method. you are using "add" – SUNIL DHAPPADHULE Jun 13 '19 at 17:29
  • @SunilDhappadhule, thanks, I was just working on that, part of the problem looks like is I needed to write it like this `Cards.Add(card);`, referencing `public List Cards = new List();` – Daniel Jun 13 '19 at 17:32
  • Your `Card` class probably ought to include a Value property. "Ace" and "Queen" are hard to evaluate for things like a straight in poker or the hand score in Blackjack. Plus, in many games card values can differ from the rank. The most common example is an Ace in Blackjack. – Ňɏssa Pøngjǣrdenlarp Jun 13 '19 at 17:59

1 Answers1

4

cards is null. You should initialize it, as was done in the commented-out line above it.

Curt Nichols
  • 2,757
  • 1
  • 17
  • 24