An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

23,278

I think error line is here:

return filterList[filterList.FindIndex(delegate(ProcessInformation proc)
    {
        return proc.name.Equals(applicationNameList[countApplicationList.IndexOf(max)], StringComparison.Ordinal);
    })];

Because List<T>.FindIndex can return -1 when you can't find index.

Instead you should test if the index is less than 0, which indicates there is an error, before you use it:

int result = filterList.FindIndex(delegate(ProcessInformation proc)
        {
            return proc.name.Equals(applicationNameList[countApplicationList.IndexOf(max)], StringComparison.Ordinal);
        });

if(result < 0) throw new Exception("Cant't Find ProcessInformation"); 
return  filterList[result];
Share:
23,278
Admin
Author by

Admin

Updated on July 25, 2022

Comments

  • Admin
    Admin almost 2 years

    In the following code, I got following error.

    An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

    Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

    Here is code:

    public ProcessInformation GetMaxRunTimeForApplicationsBetween(DateTime StartingTime, DateTime EndingTime)
        {
    
            //Filter Based on Timer
            List<ProcessInformation> filterList = new List<ProcessInformation>();
    
            foreach (HarvestApp.ProcessInformation item in this.ProcessList)
            {
                if (item.started_at.CompareTo(StartingTime) >= 0 && item.ended_at.CompareTo(EndingTime) <= 0)
                {
                    filterList.Add(item);
                }
            }
    
            //Count Max Occurrence of Application
            List<int> countApplicationList = new List<int>();
            List<string> applicationNameList = new List<string>();
            
    
            foreach (HarvestApp.ProcessInformation item in filterList)
            {
                if (applicationNameList.Contains(item.name))
                {
                    countApplicationList[applicationNameList.IndexOf(item.name)]++;
                }
                else
                {
                    applicationNameList.Add(item.name);
                    countApplicationList.Add(1);
                    
                }
            }
    
    
            //if (countApplicationList.Count == 0)
            //{
            //    throw new InvalidOperationException("Empty list");
            //}
    
    
            int max = int.MinValue;
            foreach (int item in countApplicationList)
            {
                if (item > max)
                {
                    max = item;
                }
             
            }
            
                //Return corresponding ProcessInformation Class of applicationNameList
                return filterList[filterList.FindIndex(delegate
                    (ProcessInformation proc)
                    {
                        return proc.name.Equals(applicationNameList[countApplicationList.IndexOf(max)], StringComparison.Ordinal);
                    })];
    
           
    
    
        }
    
    • Damien_The_Unbeliever
      Damien_The_Unbeliever about 11 years
      Guessing C# from the exception name and the code style, but if not, please re-tag with an appropriate language tag.
    • Mike Perrenoud
      Mike Perrenoud about 11 years
      This exception could occur in a number of places in this code. What line is it failing on? You know that much.
    • Admin
      Admin about 11 years
      exception occurrs at return statement.
    • Andre
      Andre about 11 years
      What about the stacktrace?
  • Admin
    Admin about 11 years
    I tried this. But still gives me same exception. It shows problem at Return statement.
  • Admin
    Admin about 11 years
    Yes. Error in return statement.