-1

I have a function that calls a GET request and should return a string.

public async Task<string> GetMyRequestAsync()
    {

        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", this._bearer);
        var response = client.GetAsync(this._url).Result;

        if (response.IsSuccessStatusCode)
        {
            var resultString = await response.Content.ReadAsStringAsync();
            var result = JsonConvert.DeserializeObject<string>(resultString);
            return result;
        }

        return null;
    }

However, the program stops and returns a NullReferenceException error when I call client.GetAsync(). I keep checking what could be null on the debugger but it just stops before it could reach the if statement. I checked the endpoint on Postman and it works fine.

Just to see if it would work, I replaced the bearer and url variables with hard coded strings and it still failed. What am I doing wrong here?

EDIT: I know what a NullReferenceException is, and what is causing it. I've read the linked duplicate thread, and multiple other questions referring to this problem before I posted. It wasn't helpful in my case so I posted my own question.

jelmarose
  • 11
  • 4
  • 1
    Are you sure `this.url` is not null? Where is exactly throwing the NullReferenceException? Tips: dispose HttpClient (wrap it into a `using` statement) and instead of that `.Result`, use `await cliente.GetAsync(this._url);` – Gonzo345 Oct 14 '19 at 07:07
  • The linked duplicate seems a bit erroneous. Also, the OP explicitly said they replaced the url variable with a hardcoded string... That said, I would suggest looking at the second half of @Gonzo345 above - check out the return value before calling result. – chill94 Oct 14 '19 at 07:29
  • @Gonzo345 I double checked it using breakpoints, and `this._url` is not null. I replaced the `.Result` with your suggestion and for some reason it works now. Any idea as to why this happens? I've used `.Result` on other modules of this current project and it works fine. – jelmarose Oct 14 '19 at 07:29
  • @jelmarose glad to read it was useful to you! The solution you've got there leads to an article which is quite interesting regarding best async/await uses. – Gonzo345 Oct 14 '19 at 07:50

1 Answers1

-2

Please double check this._url.

Also don't use .Result in your var response = client.GetAsync(this._url).Result; call. You are already using async/await, so replace the string with

var response = await client.GetAsync(this._url);

Read about Async Antipatterns here.

  • "What's about this._url? Is it null or not?" Don´t post **questions** into **answers**. Either you *know* the answer, or you *don't*. – MakePeaceGreatAgain Oct 14 '19 at 07:13
  • @HimBromBeere, it was not a question actually, it was a suggestion what the author should to check. Also I don't have enough reputation to post comments instead of answers. Anyway, I edited my answer, thanks! – Alexander Alekseev Oct 14 '19 at 07:18
  • I already did check using breakpoints, and as I said on my question I did replace the variable with "http://thisIsTheEndpointUrl" and it still failed. – jelmarose Oct 14 '19 at 07:24