-7

I have created a class and a global variable named as telephoneNumber. This variable is set in a method and used in another method. However this variable returns null. All methods and this global variable in the same class. Please help to understand this problem. Thanks a lot. My class is :

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; }
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
olyanren
  • 1,448
  • 4
  • 24
  • 42

4 Answers4

3

So, where you call GetSingleLocationInfo, you are calling an async method. GetSingleLocationInfo calwill therefore run as far as the await statement then return stright to the caller, before the it httpClient.GetStringAsync(hereNetUrl); has returned.

To fix this, you need to await on your call GetSingleLocationInfo before trying to access the variable.

Justin Harvey
  • 14,446
  • 2
  • 27
  • 30
1

Since GetSingleLocationInfo is async it will be called asynchronously, so the Console.WriteLine (telephoneNumber); will be called before the GetSingleLocationInfo change it. I think you should put an await when calling the method.

JoGo
  • 796
  • 5
  • 5
0

String.Substring returns NULL when no string found.

Nahum
  • 6,959
  • 12
  • 48
  • 69
0

It's going to be "returning" null quite simply because telephoneNumber hasn't been set yet.

Your declaration of the variable private string telephoneNumber; doesn't set any value thus it is an empty string or null.

My guess would be your method where you print it out is being called before the method where you actually set telephoneNumber to have a value.

Clint
  • 6,133
  • 2
  • 27
  • 48
  • No you are wrong because i set telephoneNumber in GetSingleLocationInfo method – olyanren Apr 07 '13 at 15:26
  • 1
    @mucayufa that may be, but as Justin Harvey has pointed out it's an asynchronous method so you've left yourself with a race condition. This might help: http://stackoverflow.com/questions/34510/what-is-a-race-condition – Clint Apr 07 '13 at 15:28