error - System.IndexOutOfRangeException:索引超出了数组的范围

时间:2014-10-02 15:31:06

标签: c# foreach google-geocoder

我收到此错误:

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

运行此代码时:

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); 

}

我不确定它究竟意味着什么,或者我如何解决这个问题。任何输入将不胜感激。

1 个答案:

答案 0 :(得分:2)

它通常由索引运算符抛出,如x [12]。你碰巧有这个:Results[0]。所以它必须意味着数组实际上是空的,它甚至没有零索引。顺便说一句,在调试器中运行代码,看看抛出异常的确切位置。