1

I'm working on a Xamarin.Forms application in which I'm downloading some data from the server and showing them on the screen. This data is downloaded every 5-10 seconds in order to keep the application updated. My code for the data download looks like this:

public async Task<List<string>> RefreshProgressPageAsync()
    {
        var uri = new Uri(string.Format("http://someurladdress1"));
        List<string> returnValue = new List<string>();
        try
        {
            var response = await client.GetAsync(uri);
            string allResult = string.Empty;
            if (response.IsSuccessStatusCode)
            {
                var result = await response.Content.ReadAsStringAsync();
                string[] valuePartsArray = result.Split(',');
                string id = valuePartsArray[0].Substring(valuePartsArray[0].IndexOf(':') + 1);
                string name = valuePartsArray[1].Substring(valuePartsArray[1].IndexOf(':') + 1);
                string value = valuePartsArray[2].Substring(valuePartsArray[2].IndexOf(':') + 1);
                returnValue.Add(string.Format("{0}|{1}|{2}", id, name, value));
            }

            uri = new Uri(string.Format("http://someurladdress2"));
            response = await client.GetAsync(uri);
            if (response.IsSuccessStatusCode)
            {
                var result = await response.Content.ReadAsStringAsync();
                string[] valuePartsArray = result.Split(',');
                string id = valuePartsArray[0].Substring(valuePartsArray[0].IndexOf(':') + 1);
                string name = valuePartsArray[1].Substring(valuePartsArray[1].IndexOf(':') + 1);
                string value = valuePartsArray[2].Substring(valuePartsArray[2].IndexOf(':') + 1);
                returnValue.Add(string.Format("{0}|{1}|{2}", id, name, value));
            }

            return returnValue;
        }
        catch (Exception ex)
        {
            Debug.WriteLine(@"                  ERROR {0}", ex.Message);
            return new List<string>();
        }
    }

Client is the HttpClient. There are two calls for the client because I want to download two different set of data in one RefreshProgressPageAsync call. Now my problem with this is when second await client.GetAsync(uri) happens, I get a null point exception somewhere in the GetAsynch call, which is not caught by the try-catch block. I do have a workaround by not parsing the string and instead send it as is, but my question is what could cause this NullPointException?

  • 1
    This may depend on where and when `client` is created. I'd recommend using dedicated local HttpClient instances for both class instead of accessing a instance from a global scope. – Filburt Feb 15 '17 at 12:06
  • @Filburt As far as I know, it's generally advised to use a single HttpClient instance, check this post for reference: https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ – Michał Żołnieruk Feb 15 '17 at 14:34
  • @MichałŻołnieruk as you suggested I'm using a single HttpClient instance during a whole app life cycle – SakacVladimir Feb 27 '17 at 22:57

0 Answers0