简体   繁体   中英

Index was outside the bounds of the array while reading from a file

I hope all of you are having a nice day. So I fixed one error of my program but there's another :/

So here's the code where I create my and read the data from a file:

  void ReadData(string fileName, Branch[] branches)
    {
        string shopsName = null;
        using (StreamReader reader = new StreamReader(@fileName))
        {
            string line = null;
            line = reader.ReadLine();
            if (line != null)
            {
                shopsName = line;
            }
            Branch tempBranches = TempBranch(branches, shopsName);
            string address = reader.ReadLine();
            string phoneNumber = reader.ReadLine();
            while (null != (line = reader.ReadLine()))
            {
                string[] values = line.Split(';');
                string facturer = values[0];
                string model = values[1];
                double capacity = double.Parse(values[2]);
                string energyClass = values[3];
                string assemblyType = values[4];
                string color = values[5];
                string attribute = values[6];
                double cost = double.Parse(values[7]);
                Fridges fridge = new Fridges(facturer, model, capacity, energyClass, assemblyType, color, attribute, cost);
                tempBranches.fridges.AddFridge(fridge);
            }
        }

And there's the code where I use the TempBranch method. The error is in this line: if (branches[i].ShopsName == shopsName) . Hopefully you can help me, cuz I was trying to fix this yesterday for 30 minutes and it still wasn't working :D

private static Branch TempBranch(Branch[] branches, string shopsName)
    {
        for (int i = 0; i < MaxNumberOfFridges; i++)
        {
            if (branches[i].ShopsName == shopsName)
            {
                return branches[i];
            }
        }
        return null;
    }

If you replace MaxNumberOfFridges with branches.Length it will only try to find a Branch that's within the range of the branches array. The reason it's not working is because you're trying to access an index which is greater than the Length of the array.

Try this one. Use foreach, if you dont know the lenght of array.

private static Branch TempBranch(Branch[] branches, string shopsName)
    {
        foreach(var branch in branches)
        {
            if (branch.ShopsName == shopsName)
            {
                return branch;
            }
        }
        return null;
    }

This raised error because MaxNumberOfFridges is bigger than branches length .. to simplify it, assume MaxNumberOfFridges is 20 but arry length is 10, so you are trying to access element 11 in array which is outside of array length.

to fix it

  for (int i = 0; i < branches.Length; i++)
    {
        if (branches[i].ShopsName == shopsName)
        {
            return branches[i];
        }
    }

other option is to use foreach loop

foreach(var b in branches)
{
    if (b.ShopsName == shopsName)
    {
        return branches[i];
    }
}

You can also try to make use of a LINQ query,

return branches.Where(b => b.ShopsName == shopsName).FirstOrDefault(); 

EDIT:

To NullReferenceError which occurs in your new post occurs due to null being returned in your function where your shop gets created. This due to not finding the given shopname.

So it tries to add an fridge to an shop which does not exist, which is not possible. You will have to add a check so that this does not occur.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM