Member Avatar for Jack1312

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

Heres the code for the void:

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

You didnt specify where exactly this error occurs, but i assume inthe for loop. Try to change it to:

if (!line.StartsWith("!") && line != "" && !line.StartsWith("#"))
{
     //your code...
}
Member Avatar for Jack1312

The error occurs when it checks if the file exists.

In 1st if statement (If(File.Exists())?? here?
Cant be here, because this line of code does not throw any error. It returns a boolen value (true if file exist, and false if files does NOT exist).
So no error possible.
Tell me where EXACTLY does this happen. Please use a debug mode.

Should line == "" then the first if (line 8) will pass to the else clause (line 111) which is testing for line[0] == "!".
This will generate an error because line is empty!
[Edit]
Try adding this at the beginning of the foreach loop.

if (string.IsNullOrWhiteSpace(line))
    continue;

[Edit2]
In fact line 8 will throw an error should line == "" because you test line[0] there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.