C
C#2y ago
baykkz

❔ Null instance in Array of Classes

Hey guys. It's my first time here, so sorry for any formatting errors or something like that. I'm making this program where I need to create an array of objects of a class. The compiler doesn't detect any errors, but when I try to modify an instance in the array he tells me that that instance is null.
CreateCards();

void CreateCards()
{
int colorNumber = 1;
int rankNumber = 1;
int currentCard = 1;
string color;
string rank;

Card[] cards = new Card[1];

//this is where the program crashes and the compiller tells me cards[] is null
cards[0].CardColor = CardColor.zero;
}

class Card
{
public CardColor CardColor { get; set; } = CardColor.zero;
public CardRank CardRank { get; set; } = CardRank.zero;
public bool isFace { get; }

public Card ()
{
CardColor = CardColor.zero;
CardRank = CardRank.zero;
if (CardRank == CardRank.cifrao || CardRank == CardRank.porcentagem ||
CardRank == CardRank.circunflexo || CardRank == CardRank.ampersand) isFace = true;
}
}

enum CardColor { zero, red, green, blue, yellow}
enum CardRank { zero, one, two, three, four, five, six, seven, eight, nine, ten, cifrao, porcentagem, circunflexo, ampersand}
CreateCards();

void CreateCards()
{
int colorNumber = 1;
int rankNumber = 1;
int currentCard = 1;
string color;
string rank;

Card[] cards = new Card[1];

//this is where the program crashes and the compiller tells me cards[] is null
cards[0].CardColor = CardColor.zero;
}

class Card
{
public CardColor CardColor { get; set; } = CardColor.zero;
public CardRank CardRank { get; set; } = CardRank.zero;
public bool isFace { get; }

public Card ()
{
CardColor = CardColor.zero;
CardRank = CardRank.zero;
if (CardRank == CardRank.cifrao || CardRank == CardRank.porcentagem ||
CardRank == CardRank.circunflexo || CardRank == CardRank.ampersand) isFace = true;
}
}

enum CardColor { zero, red, green, blue, yellow}
enum CardRank { zero, one, two, three, four, five, six, seven, eight, nine, ten, cifrao, porcentagem, circunflexo, ampersand}
6 Replies
Anton
Anton2y ago
yes that's how it works. read on reference types the default value for a reference type is null
baykkz
baykkzOP2y ago
and how would I define the value for all instances of the array here?
mrphil2105
mrphil21052y ago
When you new up an array you get an array of the default value The default value of a class (aka reference type) is null So you need to go through the array and initialize
for (int i = 0; i < arr.Length; i++)
{
arr[i] = new Foo();
}
for (int i = 0; i < arr.Length; i++)
{
arr[i] = new Foo();
}
Foo being Card here What you are doing now is accessing something on null, not valid
baykkz
baykkzOP2y ago
oh, think I got it let me try this this worked perfectly!! Also got how it works Thank you so much @mrphil2105 !!
mrphil2105
mrphil21052y ago
👍
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server