سؤال

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?

هل كانت مفيدة؟

المحلول

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) {...}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top