We're making changes to the Community, so you may have received some notifications - thanks for your patience and welcome back. Learn more here.

Forum Discussion

Lukag's avatar
Lukag
Collaborator | Level 8
3 months ago

Error reading file async

I had a problem to download a file ( an Image ) and someone recommended to me to do asynchronously.

I changed my code so : 

 Async Function _take_file(ByVal _file As String) As Task

....

Dim client As HttpClient = New HttpClient()

Dim _Request As HttpRequestMessage = New HttpRequestMessage(HttpMethod.Post, "https://content.dropboxapi.com/2/files/download")
_Request.Headers.Add("Authorization", "Bearer " + _token)
_Request.Headers.Add("Dropbox-API-Arg", _parametro)

Dim _response As HttpResponseMessage = Await client.SendAsync(_Request)

Dim _responseStream As Stream = Await _response.Content.ReadAsStreamAsync()
Dim myStreamReader As New StreamReader(_responseStream)

 

I have an error on the last line : 

System.NullReferenceException was unhandled
Message: An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll

  • Здравко's avatar
    Здравко
    Legendary | Level 20

    Lukag wrote:

    ...
    Dim myStreamReader As New StreamReader(_responseStream)

    ...


    Hm..🤔 Let's see what's going on here. Does the 'myStreamReader' variable get declared here, or get value assigned, or...? 🧐

    • Lukag's avatar
      Lukag
      Collaborator | Level 8

      Hm both ? 

      i thought that it  declared and that got value. 

  • Здравко's avatar
    Здравко
    Legendary | Level 20

    Lukag wrote:

    ...

    Dim client As HttpClient = New HttpClient()

    ...
    Dim myStreamReader As New StreamReader(_responseStream)

    ...


    Then, what's different and common in above code lines (aside of different types)?

    • Lukag's avatar
      Lukag
      Collaborator | Level 8

      In first case a define the object and i create as a new httpclient

      in the second i declare ad object as a new streamreader using data from response .

      In the code of example i see i thought it works so 🙂 

      • Здравко's avatar
        Здравко
        Legendary | Level 20

        Your error message means that in your code you have used something not initialized at that time. That's it.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,875 PostsLatest Activity: 21 days ago
323 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!