Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

Async MoveNext() Object reference is not set to an instance of an object.

$
0
0

Hi There i am getting the following exception on my Iphone 6s device. The same code works fine on android and IOS emulator + android device.

Object reference is not set to an instance of an object at
MyApp.Services.AuthenticationService+
C_Async0.MoveNext()

The AuthenticationUser method of the AuthenticationService is a async method as below.

  public async Task<bool> AuthenticationUser(string userName, string password)
        {
            var logger = DependencyService.Get<ILogger>();

            var token = await _restClient.GetAuthenticationToken(userName, password);

            if (token == null) return false;

            Settings.Current.SignIn(token.access_token);

            return true;
        }

The exception is being thrown at

var token = await _restClient.GetAuthenticationToken(userName, password);

Here is the GetAuthenticationToken method

public async Task<OAuthResponse> GetAuthenticationToken(string emailAddress, string password)
        {
            OAuthResponse oAuthResponse;


            using (var client = new HttpClient())
            {

                //setup client
                client.BaseAddress = new Uri(ApiSettings.ApiBaseUrl);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                //setup login data
                var formContent = new FormUrlEncodedContent(new[]
                {
                        new KeyValuePair<string, string>("grant_type", "password"),
                        new KeyValuePair<string, string>("username", emailAddress),
                        new KeyValuePair<string, string>("password", password),
                 });

                //send request
                HttpResponseMessage responseMessage = await client.PostAsync(ApiResource.OAuthTokenResource, formContent);
                //get access token from response body

                var responseJson = await responseMessage.Content.ReadAsStringAsync();
                oAuthResponse = JsonConvert.DeserializeObject<OAuthResponse>(responseJson);
                if (!responseMessage.IsSuccessStatusCode)
                {
                    throw new InvalidCredentialException<OAuthResponse>(oAuthResponse);
                }
            }
            return oAuthResponse;
        }

What is very strange is that the null exception is only thrown on IOS Device. This code is working fine on android and IOS emulator . The issue is just with IOS device.


Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>