-2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace TacticalCheeseRacer
{
    class Program
    {
        static int playerNum;
        static int totalPlayers;
        static Player[] players = new Player[4];

        struct Player
        {
            public string Name;
            public int Pos;
        }

        private static void PlayerTurn(int playerNo, int distance)
        {
            // TODO: Makes a move for the given player
        }

        static void ResetGame()
        {
            // TODO: get the number of players and set their positions at 0
        }

        static void GameTurn()
        {
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Enter total number of players for game:");
            totalPlayers = int.Parse(Console.ReadLine());

            for (int i = 0; i < totalPlayers; i++)
            {
                Player p;
                Console.WriteLine("Enter player's details:");
                Console.WriteLine("Name:");
                p.Name = Console.ReadLine();
                Console.WriteLine("Position:");
                p.Pos = int.Parse(Console.ReadLine());
                players[i] = p;
                playerNum++;

            } //while (playerNum < totalPlayers);

            for (int i = 0; i < totalPlayers; i++)
            {
                Console.WriteLine(players[i]); // test to see if it works
            }
        }
    }
    // store player positions in arrays
}

Unhandled Exception: System.ArgumentNullException: Argument cannot be null.
Parameter name: s
  at System.Int32.Parse (System.String s) [0x00000] in <filename unknown>:0 
  at TacticalCheeseRacer.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0

Can't seem to find the error any1 have any ideas?

Vikas Gupta
  • 4,455
  • 1
  • 20
  • 40
Theepicpowner
  • 17
  • 1
  • 6
  • 1
    What have you tried to debug the issue? Have you stepped though the code placing a breakpoints on the lines: `totalPlayers = int.Parse(Console.ReadLine());` and `p.Pos = int.Parse(Console.ReadLine());`? – Chris Moutray Nov 20 '14 at 06:20
  • Next time put for us a compilable code. –  Nov 20 '14 at 06:26

1 Answers1

1

So your issue is in the line:

totalPlayers = int.Parse(Console.ReadLine());

As seen by your error. The easiest way to fix this is as follows:

if(int.TryParse(Console.ReadLine(), out totalPlayers)) {
   // continue your normal logic here
} else {
   // uh oh, couldn't parse the number show a message to the user and exit gracefully
}

With int.TryParse you are trying to parse and if successful you continue on with your totalPlayers being set properly. If it isn't successful, then you can exit gracefully rather than have an exception thrown.

Bottom line, if you are getting the input from the user you can't trust it so you should use TryParse so that they can't cause a problem with bad data.

Vikas Gupta
  • 4,455
  • 1
  • 20
  • 40
Jetti
  • 2,418
  • 1
  • 17
  • 25