2013-04-07 85 views
-7

我已經創建了一個名爲telephoneNumber的類和一個全局變量。這個變量是在一個方法中設置的,並在另一個方法中使用。但是這個變量返回null。所有方法和這個全局變量在同一個類中。請幫助理解這個問題。非常感謝。我的課程是:爲什麼實例變量在C#中返回null?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Net; 
using System.IO; 
using System.Net.Http; 
using Newtonsoft.Json; 
using System.Collections; 
namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     private string telephoneNumber; 
     private async void GetSingleLocationInfo(string href) 
     { 

      var hereNetUrl = string.Format(
       href+"&accept=application/json" 
        ); 

      // get data from HERE.net REST API 
      var httpClient = new HttpClient(); 
      var hereNetResponse = await httpClient.GetStringAsync(hereNetUrl); 

      // deseralize JSON from Here.net 
      using (var tr = new StringReader(hereNetResponse)) 
      using (var jr = new JsonTextReader(tr)) 
      { 
       var rootObjectResponse = new JsonSerializer().Deserialize<Object>(jr); 

       String contacts = rootObjectResponse.ToString(); 
       int startIndex = contacts.IndexOf("phone"); 
       if (startIndex != -1) 
       { 
        String value = contacts.Substring(startIndex, 50); 
        telephoneNumber=value.Substring(value.IndexOf("+")); 
       } 
       else 
       { 
        telephoneNumber=""; 
       } 

      } 
     } 
     private async void GeocodingWin8Query() 
     { 
      // build URL for Here.net REST service 
      string currentgeoLoc = "37.75075,-122.393472"; 
      string queryString = "taxi"; 
      string appID = "dV04O71v5F3f2W"; // MAKE SURE TO GET YOUR OWN from developers.here.net 
      object appCode = "8QVr5uSXwfcowDrA"; // MAKE SURE TO GET YOUR OWN from developers.here.net 
      var hereNetUrl = string.Format(
       "http://demo.places.nlp.nokia.com/places/v1/discover/search?at={0}&q={1}&app_id={2}&app_code={3}&accept=application/json", 
        currentgeoLoc, queryString, appID, appCode); 

      // get data from HERE.net REST API 
      var httpClient = new HttpClient(); 
      var hereNetResponse = await httpClient.GetStringAsync(hereNetUrl); 

      // deseralize JSON from Here.net 
      using (var tr = new StringReader(hereNetResponse)) 
      using (var jr = new JsonTextReader(tr)) 
      { 
       var rootObjectResponse = new JsonSerializer().Deserialize<RootObject>(jr); 


       List<Item> items=rootObjectResponse.results.items; 


       foreach(Item item in items){ 
        string href = item.href; 
        GetSingleLocationInfo(href); 
        Console.WriteLine (telephoneNumber);//returns null 
       } 


      } 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      GeocodingWin8Query(); 
     } 
    } 
    public class Category 
    { 
     public string id { get; set; } 
     public string title { get; set; } 
     public string href { get; set; } 
     public string type { get; set; } 
    } 

    public class Item 
    { 
     public List<double> position { get; set; } 
     public int distance { get; set; } 
     public string title { get; set; } 
     public Category category { get; set; } 
     public string icon { get; set; } 
     public string vicinity { get; set; } 
     public List<object> having { get; set; } 
     public string type { get; set; } 
     public string href { get; set; } 
     public string id { get; set; } 
     public double? averageRating { get; set; } 
    } 


    public class Context 
    { 
     public Location location { get; set; } 
     public string type { get; set; } 
    } 

    public class Search 
    { 
     public Context context { get; set; } 
    } 

    public class RootObject 
    { 
     public Results results { get; set; } 
     public Search search { get; set; } 
    } 
} 
+3

它在哪裏返回null?你已經嘗試了什麼?你究竟想達到什麼目的? – Clint 2013-04-07 15:19:12

+0

Clint的評論借調 - 你有沒有嘗試調試任何代碼來看看telephoneNumber被設置在哪裏? – 2013-04-07 15:20:07

+0

把斷點放在你的變量賦值的地方。確保它沒有得到空值。 – ankurtr 2013-04-07 15:20:21

回答

3

所以,在你調用GetSingleLocationInfo的地方,你正在調用一個異步方法。因此,GetSingleLocationInfo calwill將運行至await語句,然後在它的httpClient.GetStringAsync(hereNetUrl)之前將stright返回給調用者;已經返回。

要解決此問題,您需要在嘗試訪問變量之前等待您的調用GetSingleLocationInfo。

+0

你是對的。問題是因爲我不用等待。謝謝 – olyanren 2013-04-07 15:28:40

0

String.Substring在找不到字符串時返回NULL。

0

這將是「返回」null很簡單,因爲telephoneNumber尚未設置。

您聲明的變量private string telephoneNumber;未設置任何值,因此它是空字符串或null。

我的猜測是你打印出來的方法是在你實際設置telephoneNumber有一個值的方法之前調用。

+0

不,你錯了,因爲我在GetSingleLocationInfo方法 – olyanren 2013-04-07 15:26:22

+1

@mucayufa中設置了telephoneNumber,可能是這樣,但正如Justin Harvey指出的那樣,它是一種異步方法,所以你給自己留下了一個競爭條件。這可能有所幫助:http://stackoverflow.com/questions/34510/what-is-a-race-condition – Clint 2013-04-07 15:28:51

1

由於GetSingleLocationInfo是異步它將被異步調用,所以Console.WriteLine(telephoneNumber);將在調用GetSingleLocationInfo之前調用它。 我想你應該把等待時調用該方法。