Advertisement

I don't understand this error

Started by April 13, 2022 04:55 PM
4 comments, last by frob 2 years, 8 months ago

Hello I'm a newbie in programming and I'm blocking on this error :

System.IndexOutOfRangeException: Index was outside the bounds of the array.
 at test.Program.Main (System.String[] args) [0x0009b] in <e574cc420fd948179f010210746876b3>:0 

Any idea where does this could come from ?

Context: I develop a c# program on the console where the user will write a x,y positions + coins positions and the algorithm have to count if the user walk through coins (after the player write different movement indications that will influence x and y).

And here's the code :

class Program
    {
        static void Main(string[] args)
        {
            /* Get data from input file */
            var data = new List<String>();
            string line;

			while ((line = Console.ReadLine()) != null) {
				data.Add(line);
			}
            ////////////////////////////////////////////////////
            int N,x,y,D,P,pCollecte;

            N = Int32.Parse(data[0]);

            P = Int32.Parse(data[1]);
            //pLieu is the location of the coins
            Dictionary<string, string> pLieu = new Dictionary<string, string>();
            for (int i = 0; i < P; i++)
            {
                pLieu.Add( "p" + i, data[2 + i]);
            }

            string ligne3;
            ligne3 = data[3 + P];
            string[] ligne3Num = ligne3.Split(',');
            x = Int32.Parse(ligne3Num[0]);
            y = Int32.Parse(ligne3Num[1]);

            D = Int32.Parse(data[4 + P]);
            Dictionary<string, string> Instructions = new Dictionary<string, string>();
            for (int i = 0; i < D; i++)
            {
                Instructions.Add("d" + i, data[5 + P + i]);
            }
            string location;
            // pcollecte are the number of coins collected
            pCollecte = 0; 
            foreach (string item in Instructions.Values)
            {   
                if (x >= N || x <= -N || y >= N || y <= -N)
                {
                    break;
                }

                location = x + "," + y;

                
                if (pLieu.ContainsKey(location))
                {
                    pCollecte++;
                    pLieu.Remove(location);
                }
                
                switch (item.ToLower())
                {
                    case "d":
                        y--;
                        break;
                    case "u":
                        y++;
                        break;
                    case "l":
                        x--;
                        break;
                    case "r":
                        x++;
                        break;
                    case "s":
                        x = x;
                        break;
                }
            }
            if (x >= N || x <= -N || y >= N || y <= -N)
            {
                Console.WriteLine("out");
            } else
            {
             Console.WriteLine(pCollecte);
            }
           
        	
        }
    }

Does the error not give a line-number?

At a guess, I might suggest adding a print-statement within the first “while”-loop: I'm dubious that “(line = Console.ReadLine())” will even be other than "null"--as it's testing the result of an assignment, rather than the value being assigned--and thus your “data” array might be empty when you attempt to access it just after.

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

Advertisement

@Thaumaturge First of all thank you. And unfortunately that's the problem there's no line number to help me :')

But it's impossible that the error come from what you suggested because, I forgot to mention this is an exercice and all that is written before the “///////////////////////////////” line isn't written by me.

All right, in that case I'd suggest peppering your code with print-outs in order to determine where the problem is happening. Once you know where to look, the source of the problem may be more apparent! (Or at least easier to find, as you have less code to comb through.)

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

Chocolatine said:
I forgot to mention this is an exercice and all that is written before the “///////////////////////////////” line isn't written by me.

Homework, then?

This is something your debugger is great at. Now is the perfect time to learn how to use it.

Assuming you're in visual studio drop a breakpoint at the beginning, either by pressing F9 on the line, clicking in the right gutter, using the menus, or right clicking to create the breakpoint. Step through the program one command at a time with F11. Adjust the hotkeys if necessary to different debuggers. Check the results every step of the way to ensure they're doing what you think they're doing.

This topic is closed to new replies.

Advertisement