Index was outside the bounds of the array

11/12/2024 01:45 denominator#1
Code:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at Conquer.Server.LoadNewMonsters(UInt32 id) in D:\Conquer\Conquer\System\Server.cs:line 477
Code:
        public static void LoadNewMonsters(uint id)
        {
            try
            {
                Game.MsgMonster.MobCollection colletion = new Game.MsgMonster.MobCollection(id);
                if (colletion.ReadMap())
                {
                    string[] text = System.IO.File.ReadAllLines(Program.ServerConfig.DbLocation + "MonsterSpawns\\NewMonster.txt");
                    foreach (string line in text)
                    {
                        string[] data = line.Split(' ');
                        ushort MapID = (ushort)long.Parse(data[1]);// Line 477
                        if (id != MapID) continue;
                        ushort X = (ushort)long.Parse(data[2]);
                        ushort Y = (ushort)long.Parse(data[3]);
                        ushort XPlus = (ushort)long.Parse(data[4]);
                        ushort YPlus = (ushort)long.Parse(data[5]);
                        uint respawn = (uint)long.Parse(data[6]);
                        int Amount = (int)long.Parse(data[7]);
                        uint monsterID = (uint)long.Parse(data[8]);
                        Game.MsgMonster.MonsterFamily famil;
                        if (!Pool.MonsterFamilies.TryGetValue(monsterID, out famil))
                        {
                            continue;
                        }
                        if (Game.MsgMonster.MonsterRole.SpecialMonsters.Contains(famil.ID))
                            continue;
                        Game.MsgMonster.MonsterFamily Monster = famil.Copy();
                        Monster.SpawnX = X;
                        Monster.SpawnY = Y;
                        Monster.MaxSpawnX = (ushort)(X + XPlus);
                        Monster.MaxSpawnY = (ushort)(Y + YPlus);
                        Monster.MapID = MapID;
                        Monster.SpawnCount = (byte)Amount;
                        Monster.rest_secs = (int)respawn;
                        colletion.Add(Monster);
                    }
                }

                GC.Collect();
            }
            catch (Exception e) { MyConsole.WriteLine(e.ToString()); }
        }
Example of NewMonster.txt

Code:
8000 15757 144 143 50 50 5 15 2437
Can I get a laymans terms of what it means, is it the code (doubtful) or is it the .txt file that's the issue? I say doubtful it's the code because the code before is exactly the same
Code:
public static void LoadMyMonsters(uint id)
But works with no error
11/12/2024 10:06 BarcodeReader#2
The text file is the issue.

The error specifically tells you that an array index (in this case an index to "data") was out of bounds, which means that the index was above the size of the array.

So when you are parsing the file then a certain line in it contains invalid data.

Just figure out what line is causing the issue and then either remove it or fix it.

I'm going to guess and say that you have an empty line somewhere, perhaps at the end of the file.
11/12/2024 13:38 pintinho12#3
For some reason, some inis and dats from TQ will sometimes have less or more columns, and it may happen to get this error or a conversion error. Just find the problem row using a try catch block or reading the file urself.
11/12/2024 21:05 denominator#4
Thanks, yeah just checked the file and half way through there was missing data
11/13/2024 00:22 Arcо#5
Layman's terms:
The basket was created to hold 5 pieces of bread.
Bread1
Bread2
Bread3
Bread4
Bread5

You tried to grab Bread6.
The basket exploded.
11/13/2024 01:09 denominator#6
Quote:
Originally Posted by Arcо View Post
Layman's terms:
The basket was created to hold 5 pieces of bread.
Bread1
Bread2
Bread3
Bread4
Bread5

You tried to grab Bread6.
Yeah that's what I had assumed, I initially thought it was the code so tried changing the number given, then after some thought assumed it was something to do with the txt that the code was reading. Turns out there was an empty line in the txt
11/13/2024 13:58 TheFadedOne#7
Quote:
Originally Posted by Arcо View Post
Layman's terms:
The basket was created to hold 5 pieces of bread.
Bread1
Bread2
Bread3
Bread4
Bread5

You tried to grab Bread6.
The basket exploded.
hahaha this is how id explain coding to my daughter, made me laugh
11/13/2024 17:42 Soulfly25#8
Just remove this "15757" in your sample txt.