2014-11-21 32 views
1

我越來越指數爲數組例外的界限之外在這條線System.IndexOutOfRangeException:索引是該數組地理編碼的API

string strLat = myCoordenates.Results[0].Geometry.Location.Lat.ToString(); 

這應該是從地理編碼拉緯度的邊界之外請求並將其變成一個字符串。

這裏是我使用的地理編碼類,我是從這裏:How to store geocoded address information into the database

using System.Net; 
using System.Runtime.Serialization; 
using System.Runtime.Serialization.Json; 
using System.Web; 

public class GoogleMapsDll 
{ 
    public class GoogleMaps 
    { 
     /// <summary> 
     /// 
     /// </summary> 
     /// <param name="address"></param> 
     /// <returns></returns> 
     public static GeoResponse GetGeoCodedResults(string address) 
     { 
      string url = string.Format(
        "http://maps.google.com/maps/api/geocode/json?address={0}&region=dk&sensor=false", 
        HttpUtility.UrlEncode(address) 
        ); 
      var request = (HttpWebRequest)HttpWebRequest.Create(url); 
      request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate"); 
      request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; 
      DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(GeoResponse)); 
      var res = (GeoResponse)serializer.ReadObject(request.GetResponse().GetResponseStream()); 
      return res; 
     } 
    } 

    [DataContract] 
    public class GeoResponse 
    { 
     [DataMember(Name = "status")] 
     public string Status { get; set; } 

     [DataMember(Name = "results")] 
     public CResult[] Results { get; set; } 

     [DataContract] 
     public class CResult 
     { 
      [DataMember(Name = "geometry")] 
      public CGeometry Geometry { get; set; } 

      [DataContract] 
      public class CGeometry 
      { 
       [DataMember(Name = "location")] 
       public CLocation Location { get; set; } 

       [DataContract] 
       public class CLocation 
       { 
        [DataMember(Name = "lat")] 
        public double Lat { get; set; } 

        [DataMember(Name = "lng")] 
        public double Lng { get; set; } 
       } 
      } 
     } 

     public GeoResponse() 
     { } 
    } 
} 

我讀過這件事(What is an IndexOutOfRangeException/ArgumentOutOfRangeException and how do I fix it?)和它的部分多次,但我仍然不知道如何解決這個問題。我覺得也許這可能是因爲我沒有從我的api請求中得到任何結果,但我不確定如何確定。

+0

第四個元素在引發錯誤,檢查你的局部變量,看看什麼是存儲在myCoordenates.Results的例子。看起來你的API沒有任何返回。 – Michael 2014-11-21 15:48:33

回答

1

很明顯,您會遇到陣列異常的界限,因爲您嘗試訪問myCoordenates.Results[0],但myCoordenates.Results沒有元素。總是檢查myCoordenates.Results是否爲空,並且如果myCoordenates.Results包含訪問myCoordenates.Results[0]之前的任何元素。你還需要檢查是否myCoordenates.Results[0].Geometry爲null,如果myCoordenates.Results[0].Geometry.Location爲null,以避免NullReferenceException,這也解釋了這裏:What is a NullReferenceException and how do I fix it?

if (myCoordenates.Results != null && myCoordenates.Results.Length > 0) 
{ 
    if (myCoordenates.Results[0].Geometry != null 
     && myCoordenates.Results[0].Geometry.Location != null) 
    { 
     string strLat = myCoordenates.Results[0].Geometry.Location.Lat.ToString(); 
    } 
    else 
    { 
     // logic when myCoordenates.Results[0].Geometry is null or 
     // myCoordenates.Results[0].Geometry.Location is null 
    } 
} 
else 
{ 
    // logic when myCoordenates.Results is null or myCoordenates.Results doesn't 
    // have any elements 
} 

一般來說,當myCoordenates.Results不爲空,你必須經常檢查是否myCoordenates.Results.Lengthn大如果你想訪問myCoordenates.Results[n]。以下是如果您要訪問的myCoordenates.Results

if (myCoordenates.Results != null && myCoordenates.Results.Length > 3) 
{ 
    if (myCoordenates.Results[3].Geometry != null 
     && myCoordenates.Results[3].Geometry.Location != null) 
    { 
     string strLat = myCoordenates.Results[3].Geometry.Location.Lat.ToString(); 
    } 
    else 
    { 
     // logic when myCoordenates.Results[3].Geometry is null or 
     // myCoordenates.Results[3].Geometry.Location is null 
    } 
} 
else 
{ 
    // logic when myCoordenates.Results is null or myCoordenates.Results has 
    // less than four elements 
} 
相關問題