Question

I have a simple part of a code:

try
{
    this.client.ExecuteAsync<Answer>(request, response =>
        {
            if (response.ResponseStatus == ResponseStatus.Completed)
                callback(response.Data);
            ...
        });
}
catch (WebException ex) {...}

and it throws NullReferenceException in the if line, because the response is null. What can I do to get a message about server down?

Était-ce utile?

La solution

Just add in a null check for your response:

try
{
    this.client.ExecuteAsync<Answer>(request, response =>
        {
            if (response != null && response.ResponseStatus == ResponseStatus.Completed)
                callback(response.Data);
            else
            {
               // add logic here to handle bad case
            }
        });
}
catch (WebException ex) {...}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top