Search code examples
c#foreachgoogle-geocoder

error - System.IndexOutOfRangeException: Index was outside the bounds of the array


I get this error:

System.IndexOutOfRangeException: Index was outside the bounds of the array.

when running this code:

foreach(var row in data)
{
     string strAddr = row.ADDRESS1 + "," + row.CITY + "," + row.ST;

     GoogleMapsDll.GeoResponse myCoordenadas = new GoogleMapsDll.GeoResponse();
     myCoordenadas = GoogleMapsDll.GoogleMaps.GetGeoCodedResults(strAddr);

     string strLat = myCoordenadas.Results[0].Geometry.Location.Lat.ToString();  
     string strLong = myCoordenadas.Results[0].Geometry.Location.Lng.ToString();

     System.Threading.Thread.Sleep(10); 

}

I am not sure exactly what it means or how I might go about fixing it. Any input would be appreciated.


Solution

  • It is usually thrown by indexing operators like x[12]. You happen to have that: Results[0]. So it must mean that the array is actually empty, it doesn't even have a zero index. By the way, run your code in a debugger and see where exactly the exception is thrown.