locked
NullReferenceException in System.Threading.Tasks.Task.Schedule when using System.Net.WebClient RRS feed

  • Question

  • User117079 posted

    Hi,

    I posted another post with the same issue. There is a simple Xamarin solution on github that you can download and run here. Please do.

    It seems that calling System.Net.WebClient.DownloadDataTaskAsync in DEBUG mode on my Xperia Z3 (it works just fine with an emulator) the first time throws a NullReferenceException in System.Threading.Tasks.Task.Schedule:

    public async Task<String> DownloadString(String url)
    {
        if (UseAuth)
            webclient.Headers.Set("Authorization", TokenType + " " + AccessToken);
        else if (!UseAuth && webclient.Headers["Authorization"] != null)
            webclient.Headers.Remove("Authorization");
        String response = "";
    
        try
        {
            byte[] data = await webclient.DownloadDataTaskAsync(url);
            response = Encoding.UTF8.GetString(data);
        }
        catch (WebException e)
        {
            response = new StreamReader (e.Response.GetResponseStream ()).ReadToEnd ();
            ErrorResponse = JsonConvert.DeserializeObject<ErrorResponse> (response, settings);
    
            if (ErrorResponse.Error.Status == 401 && ErrorResponse.Error.Message == "The access token expired") {
                Console.WriteLine ("Error: " + ErrorResponse.Error.Status + " - " + ErrorResponse.Error.Message);
            }
        }
        return response;
    }
    

    The following dirty fix solves the problem, but It doesn't address the underlying problem.

    public async Task<String> DownloadString(String url)
    {
        if (UseAuth)
            webclient.Headers.Set("Authorization", TokenType + " " + AccessToken);
        else if (!UseAuth && webclient.Headers["Authorization"] != null)
            webclient.Headers.Remove("Authorization");
        String response = "";
    
        var failCount = 2;
        for (int i = 0; i <= failCount; i++) 
        {
            try
            {
                byte[] data = await webclient.DownloadDataTaskAsync(url);
                response = Encoding.UTF8.GetString(data);
            }
            catch (WebException e)
            {
                if (i == failCount) 
                {
                    response = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();
    
                    ErrorResponse = JsonConvert.DeserializeObject<ErrorResponse>(response, settings);
    
                    if (ErrorResponse.Error.Status == 401 && ErrorResponse.Error.Message == "The access token expired") 
                    {
                        Console.WriteLine ("Error: " + ErrorResponse.Error.Status + " - " + ErrorResponse.Error.Message);
                    }
                    // Break out of the loop
                    break;
                }
            }
    
        }
    
        return response;
    }
    

    System.NullReferenceException: Object reference not set to an instance of an object
    System.Threading.Tasks.Task.Schedule (Boolean throwException) [0x00000] in :0
    at System.Threading.Tasks.Task.Start (System.Threading.Tasks.TaskScheduler scheduler) [0x00000] in :0 at System.Threading.Tasks.TaskFactory.StartNew[WebRequest] (System.Func1 function, CancellationToken cancellationToken, > TaskCreationOptions creationOptions, System.Threading.Tasks.TaskScheduler scheduler) [0x00000] in <filename unknown>:0 at System.Threading.Tasks.TaskFactory.StartNew[WebRequest] (System.Func1 function) [0x00000] in :0 at System.Net.WebClient.SetupRequestAsync (System.Uri address) [0x00000] in :0 at System.Net.WebClient+casync0.MoveNext () [0x00000] in :0 --- End of inner exception stack trace ---
    at System.Net.WebClient+c
    async0.MoveNext () [0x00000] in :0 --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in :0
    at System.Runtime.CompilerServices.TaskAwaiter`1[System.Byte[]].GetResult () [0x00000] in :0
    at SpotifyWebAPI.SpotifyWebAPIClass+c__async1A.MoveNext () [0x00133] System.Net.WebException

    Here is a screenshot from Xamarin Studio

    The problem ONLY happens with my device and DEBUG build mode. In RELEASE it works perfectly and on the emulator it works on both DEBUG and RELEASE.

    As stated in the original post, calling System.WebClient.DownloadDataTaskAsync a second, third or Nth time all result in normal behaviour. It also only occur when I use a Spotify Android SDK Java binding project and call OpenLoginWindow() prior to calling DownloadDataTaskAsync.

    Please help me. I don't know how to solve this. It has been several weeks now. Thank you Fredrik

    Tuesday, April 21, 2015 3:20 PM

All replies

  • User117079 posted

    Please, would anyone be so kind to test the project? I would really appreciate it.

    Tuesday, April 21, 2015 5:45 PM