I have code something like this:
HttpResponseMessage response = new HttpResponseMessage();
try
{
string url = ConfigurationManager.AppSettings["url"];
using (HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, url))
{
requestMessage.Headers.Add("Accept", "application/json");
response = await SendHttpRequest(requestMessage).ConfigureAwait(false);
}
if (response != null && response.IsSuccessStatusCode)
{
// do other things
}
else
{
log.Error("Http response failure with Status Code : " + response.StatusCode"); // sonarqube showing a bug here
}
}
catch (Exception ex2)
{
// log the exception
}
public virtual async Task<HttpResponseMessage> SendHttpRequest(HttpRequestMessage httpRequest)
{
HttpResponseMessage response = null;
using (HttpClient client = new HttpClient())
{
response = await client.SendAsync(httpRequest).ConfigureAwait(false);
}
return response;
}
When I run Sonarqube analysis on this code, it is throwing a bug at the commented line in the code, saying "'response' is null on at least one execution path"
How can I make this go away. I have intialized HttpResponseMessage , but still this is throwing a bug.
Any help in this regard will be helpful.