0

I can't seem to get my field / list working. I want to add to the list and use the list in CheckUserInput and PlayerMove methods. It says it's not being used but I am using it in those methods without any errors.

I tried to play around with it for a bit and all the methods are static methods so I had to make a static field.

private static List<string> AlreadyUsed = new List<string>();

private static void CheckUserInput(ref int player, ref string answer, ref bool error)
    {
        AlreadyUsed.Add("");

        if(int.Parse(answer) > 9)
        {
            Console.WriteLine("You have entered a value outside the array. Skipping your turn.");
            error = true;
        }
        else
        {
            for (int i = 0; i <= 9; i++)
            {
                if (AlreadyUsed[i] == "X" || AlreadyUsed[i] == "O")
                {
                    Console.WriteLine("You have already entered a value in this slot. Skipping your turn.");
                    error = true;
                }
            }
        }
    }


private static void PlayerMove(ref string answer, string[] arr, ref int player) //make whole new class for this method?
    {
        for (int i = 0; i <= 8; i++) //make player move
        {
            if (answer == arr[i])
            {
                if (player == 1)
                {
                    arr[i] = "X";
                    AlreadyUsed.Add(answer);
                }
                else
                {
                    arr[i] = "O";
                    AlreadyUsed.Add(answer);
                }
            }
        }
    }

I used CheckUserInput before PlayerMove hence adding the extra value in the list. I was met with a runtime error.

Thank you!

L. John
  • 39
  • 6
  • That isn't what the error message says. Please share the **exact** error message. – mjwills Sep 04 '19 at 22:45
  • `for (int i = 0; i <= 9; i++)` Talk me through what you think will happen when you access the 3rd item in a list containing only 1 entry. Also have a read of https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.count?view=netframework-4.8 . – mjwills Sep 04 '19 at 22:46
  • Sorry for not putting in the error message, however I got a NullReferenceException and found the source of the problem by looking at the error message. I'm not sure how I can copy the error message but it said: Object reference not set to an instance of an object but since it's static I would've thought I didn't have to instantiate it or am I missing something here? Also can the other person see the comment I had made even-though it is marked as a duplicate as I still have a question. Thanks – L. John Sep 05 '19 at 16:26

1 Answers1

0

Looks like in CheckUserInput method you iterating the list AlreadyUsed till index 9 but your code shows that the list AlreadyUsed would not have that many items to iterate and an Index out of range exception would occur.

In that for loop all you trying to check if AlreadyUsed contains strings "X" or "O", no need for the for loop you could do that with Contains method, replace your for loop in CheckUserInput method with this code:

if (AlreadyUsed.Contains("X") || AlreadyUsed.Contains("O"))
{
  Console.WriteLine("You have already entered a value in this slot. Skipping your turn.");
  error = true;
}
Rashid Ali
  • 587
  • 3
  • 13
  • Thanks for the help, however it seems like on top of this code, I needed to set the object's reference to an instance of the object but I thought this wouldn't be required since it is static? – L. John Sep 05 '19 at 16:22
  • static class cannot be instantiated but this thing doesn't apply for a static field! – Rashid Ali Sep 05 '19 at 17:29