Click here to Skip to main content
15,921,884 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have a class file. Inside the class is a void which is started in the Program.cs. I used 'LavaSurvival.Init("properties/lava.properties")' in the Program.cs. I'm only pretty new to coding, so it's probably something I didn't know XD. The problem is that I continue to get this error:

Type: IndexOutOfRangeException
Source: Test
Message: Index was outside the bounds of the array.
Target: Init
Trace: at Test.LavaSurvival.Init(String path) in C:\mypath=3\LavaSurvival.cs:line 61

UPDATE: Line 61 is the 'if (File.Exists(path))'

Heres the code for the void:

C#
public static void Init(string path)
        {
           if (File.Exists(path))
            {
                string[] lines = File.ReadAllLines(path);
                foreach (string line in lines)
                {
                    if (line[0] != '!' && line != "" || line[0] != '#')
                    {
                        string key = line.Split('=')[0].Trim();
                        string value = line.Split('=')[1].Trim();
                        switch (key.ToLower())
                        {
                            case "lava-mode":
                                try
                                {
                                    if (value != "normal" && value != "rising")
                                    {
                                        Server.s.Log("Invalid lava mode. Using default.");
                                    }
                                    else
                                    {
                                        lavamode = value;
                                    }
                                }
                                catch { }
                                break;
                            case "lava-speed":
                                try
                                {
                                    bool failed = false;
                                    try
                                    {
                                        int.Parse(value);
                                    }
                                    catch { Server.s.Log("Invalid lava-speed. Using default"); failed = true; }
                                    if (!failed)
                                    {
                                        if (int.Parse(value) > 20)
                                        {
                                            Server.s.Log("Lava-Speed cannot excede 20. Using default.");
                                        }
                                        else if (int.Parse(value) < 1)
                                        {
                                            Server.s.Log("Lava-Speed cannot be less than 1. Using default.");
                                        }
                                        else
                                        {
                                            lavaspeed = int.Parse(value);
                                        }
                                    }
                                }
                                catch { Server.s.Log("An Error Occurred loading the lava-speed!"); }
                                break;
                            case "time-before-lava":
                                try
                                {
                                    bool failed = false;
                                    try
                                    {
                                        int.Parse(value);
                                    }
                                    catch { Server.s.Log("Invalid time before lava. Using default."); failed = true; }
                                    if (!failed)
                                    {
                                        if (int.Parse(value) > 60)
                                        {
                                            Server.s.Log("The time-before-lava cannot excede 60 minutes. Using default.");
                                        }
                                        else if (int.Parse(value) < 1)
                                        {
                                            Server.s.Log("The time-before-lava cannot be less than 1 minute. Using default.");
                                        }
                                        else
                                        {
                                            minutesbefore = int.Parse(value);
                                        }
                                    }
                                }
                                catch { Server.s.Log("An Error Occurred loading the time-before-lava!"); }
                                break;
                            case "time-after-lava":
                                try
                                {
                                    bool failed = false;
                                    try
                                    {
                                        int.Parse(value);
                                    }
                                    catch { Server.s.Log("Invalid time after lava. Using default."); failed = true; }
                                    if (!failed)
                                    {
                                        if (int.Parse(value) > 60)
                                        {
                                            Server.s.Log("The time-after-lava cannot excede 60 minutes. Using default.");
                                        }
                                        else if (int.Parse(value) < 1)
                                        {
                                            Server.s.Log("The time-after-lava cannot be less than 1 minute. Using default.");
                                        }
                                        else
                                        {
                                            minutesafter = int.Parse(value);
                                        }
                                    }
                                }
                                catch { Server.s.Log("An Error Occurred loading the time-after-lava!"); }
                                break;
                        }
                    }
                    else if (line[0] == '!')
                    {
                        bool failed = false;
                        try
                        {
                            string name = line.Split(' ')[0];
                            string x1 = line.Split(' ')[1];
                            string x2 = line.Split(' ')[2];
                            string x3 = line.Split(' ')[3];
                            if (!File.Exists("levels/" + line.Split(' ')[0].ToLower() + ".lvl"))
                            {
                                Server.s.Log("Failed to load the lavamap, " + name.ToLower() + " because the map does not exist in levels/");
                                failed = true;
                            }
                            else
                            {
                                ushort.Parse(x1);
                                ushort.Parse(x2);
                                ushort.Parse(x3);
                            }
                            if (!failed)
                            {
                                LavaSurvivalMaps.Add(name, ushort.Parse(x1), ushort.Parse(x2), ushort.Parse(x3));
                            }
                            failed = false;
                        }
                        catch { Server.s.Log("One of your settings for the map, " + line.Split(' ')[0] + " was invalid!"); failed = false; }
                    }   
                }
            }
        }



Thanks in advance, if you can help me fix this error :D
Posted
Updated 5-Sep-11 21:44pm
v2

There are several places where you could get this error, so debugger is your best friend :).
For example :

  • if line read from a file is empty, line[0] produces error
  • if line doesn't contain '=' symbol, line.Split('=')[1] produces error
  • if line contains less than three spaces, line.Split(' ')[3] produces error


Besides, I'd recommend to optimize your code a bit: use TryParse() method, create temp variables for parsed and splitted strings etc :)
 
Share this answer
 
v2
Comments
Jack1848238528 6-Sep-11 4:06am    
I tried doing both these solutions... but it still exits with the same error on checking if the file exists... 'if (File.Exists(path))' The path that was specified in the other code is "properties/lava.properties"
Timberbird 6-Sep-11 4:16am    
I'd recommend to put a breakpoint on this line and when execution stops on it, do two things:
First, check path value and ensure that it points to an existing file (btw, is path relative or absolute? consider that too).
Second, move one step further (F10 in Visual Studio) to confirm this line is really a culprit :)
After spliting the string, first check the length of the Array so that it would never cross the boundary of the Array

foreach (string line in lines)
{
    if (line.Length > 0 && line[0] != '!' && line != "" || line[0] != '#')
    {
        string key = line.Split('=')[0].Trim(); // Before accessing 0th char check the length
        string value = line.Split('=')[1].Trim(); // Before accessing 1th char check the length
 
Share this answer
 
Comments
Timberbird 6-Sep-11 3:56am    
I'd rather use two conditions: first if (line.Length > 0), then if (line[0] != '!' && line[0] != '#'). Don't like to hope on partial checks :)
BTW, I've modified condition a bit as it seems quite strange otherwise :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900