简体   繁体   中英

c# System.NullReferenceException with Jagged Array

I have:

namespace CardGame
{
    class Dealer
    {
        static void Main(string[] args)
        {
            string[] suits = { "Clubs", "Spades", "Hearts", "Diamonds" };
            string[] specials = { "Jack", "Queen", "King", "Ace" };
            string[][] hand = new string[5][];

            Console.WriteLine("Please enter the number of players:");

            int playerCount = Int32.Parse(Console.ReadLine());

            for (int currentPlayer = 0; currentPlayer < playerCount; currentPlayer++)
            {
                Random rand = new Random();
                for (int cardNumber = 0; cardNumber < 5; cardNumber++)
                {
                    string card;

                    int mode = rand.Next(0, 2);
                    if (mode == 1) // Numeric card...
                    {
                        card = rand.Next(2, 10).ToString();
                    }
                    else // Face card or ace...
                    {
                        card = specials[rand.Next(0, 4)];
                    }
                    hand[currentPlayer][cardNumber] = card += " of " + suits[rand.Next(0, 3)];
                    Console.WriteLine(card += " of " + suits[rand.Next(0, 3)]);
                }
            }
            Console.ReadLine();
        }
    }
}

The line: hand[currentPlayer][cardNumber] = card += " of " + suits[rand.Next(0, 3)];

Is throwing the error in the title. I have no clue how to fix this as I am very new to C#.

What do I need to do?

You have created a jagged array, but it's not a complete array of arrays, it's just an array of null references. You have to create an inner array for each item in the outer array.

You have created an array of five items, but it should be an array that has the length of the number of players. So, you have to create the array after you know how many players there are:

...
string[][] hand;

Console.WriteLine("Please enter the number of players:");
int playerCount = Int32.Parse(Console.ReadLine());

hand = new string[playerCount][];
...

Now, inside the loop you should create an inner array for each item, that's the array that should have the length five. The Random instance should be created outside the outer loop, if you create new instances too close in time they will generate the same number sequences.

...
Random rand = new Random();
for (int currentPlayer = 0; currentPlayer < playerCount; currentPlayer++)
{
  hand[currentPlayer] = new string[5];
  ...

As all your inner arrays have the same length, you could use a two dimensional array instead of a jagged array. It's declared in a similar way:

string[,] hand;

and it is created in a similar way:

hand = new string[playerCount, 5];

As it is a single array and not an array of arrays, you don't need to create any inner arrays in the loop.

The assignment of items is also slightly different:

hand[currentPlayer, cardNumber] = ...

I have made changes to your code and it runs. I believe that is what you would like to achieve

using System.Linq;
namespace CardGame
{
    class Dealer
    {
        static void Main(string[] args)
        {
            string[] suits = { "Clubs", "Spades", "Hearts", "Diamonds" };
            string[] specials = { "Jack", "Queen", "King", "Ace" };

            // Two dimensional arrays - I believe this is what you want to achieve, run the application 
            string[,] hand = new string[5,5];

            Console.WriteLine("Please enter the number of players:");

            int playerCount = Int32.Parse(Console.ReadLine());

            for (int currentPlayer = 0; currentPlayer < playerCount; currentPlayer++)
            {
                Random rand = new Random();
                for (int cardNumber = 0; cardNumber < 5; cardNumber++)
                {
                    string card;

                    int mode = rand.Next(0, 2);
                    if (mode == 1) // Numeric card...
                    {
                        card = rand.Next(2, 10).ToString();
                    }
                    else // Face card or ace...
                    {
                        card = specials[rand.Next(0, 4)];
                    }

                    var temp = " of " + suits[rand.Next(0, 3)];

                    if (card != null && !card.Contains(temp))
                    {
                        hand[currentPlayer, cardNumber] = card += " of " + suits[rand.Next(0, 3)];


                       Console.WriteLine(card += " of " + suits[rand.Next(0, 3)]);

                        //Result
                        Console.WriteLine("Result: {0}", hand[currentPlayer, cardNumber]);
                    }




                }
            }
            Console.ReadLine();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM