未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例

时间:2019-07-31 21:46:07

标签: xamarin.forms

在进行api调用后,如果我输入了错误的详细信息。我的应用程序不断出现空异常

我尝试使用if-else来解决它。但仍然是相同的错误

public class RemoteService
    {
        HttpClient httpClient;

        public RemoteService()
        {
            httpClient = new HttpClient();
            httpClient.BaseAddress = new Uri($"{App.BackendUrl}/");

        }


        public async Task<WeatherResponse> GetWeatherData(string query)
        {
            var weatherResponse = new WeatherResponse();
            var response = await httpClient.GetAsync($"weather?q=" + query + App.AppID);
            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();
                weatherResponse = JsonConvert.DeserializeObject<WeatherResponse>(content);
                weatherResponse.Error = false;
                return weatherResponse;
            }
            else
            {
                //await Application.Current.MainPage.DisplayAlert("Error", "City not found", "OK");
                return new WeatherResponse { Error = true };
            }

        }
    }

1 个答案:

答案 0 :(得分:0)

问题实际上出在viewmodel类上。解决了