[英]An exception of type 'System.NullReferenceException' occurred in UWP-Weather-App.exe but was not handled in user code
I've been following a tutorial of a UWP Weather application that uses an API key. 我一直在关注使用API密钥的UWP Weather应用程序的教程。
I get a System.NullReferenceException
. 我得到一个
System.NullReferenceException
。
This is the method it happens in, at the line that begins with RootObject
: 这是发生在
RootObject
开头的行中的RootObject
:
namespace UWP_Weather_App
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
RootObject myWeather = await OpenWeatherMapProxy.GetWeather(20.0, 30.0);
ResultTextBlock.Text = myWeather.name + " - " + myWeather.main.temp + " - " + myWeather.weather[0].description;
}
}
}
According to your description, I'd suppose you are following this UWP Weather tutorial . 根据您的描述,我想您正在关注此UWP Weather教程 。 I used your code with this tutorial's source code and reproduced your issue.
我将您的代码与本教程的源代码一起使用,并重现了您的问题。
The System.NullReferenceException
error occurred at the line ResultTextBlock.Text = myWeather.name + " - " + myWeather.main.temp + " - " + myWeather.weather[0].description;
System.NullReferenceException
错误发生在ResultTextBlock.Text = myWeather.name + " - " + myWeather.main.temp + " - " + myWeather.weather[0].description;
行ResultTextBlock.Text = myWeather.name + " - " + myWeather.main.temp + " - " + myWeather.weather[0].description;
This is because the URL you've used in GetWeather
method is not right. 这是因为您在
GetWeather
方法中使用的URL不正确。 The result returned by this URL doesn't contain weather information and when it is deserialized to RootObject
, most properties will be null. 该URL返回的结果不包含天气信息,当将其反序列化为
RootObject
,大多数属性将为null。
To retrieve current weather data by geographic coordinates, we can use following API: 要通过地理坐标检索当前的天气数据,我们可以使用以下API:
api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}
And starting from 9 October 2015 this API requires a valid APPID for access. 从2015年10月9日开始,此API需要有效的APPID才能访问。 For more info, please see Call current weather data for one location and How to use API key in API call .
有关更多信息,请参阅调用一个位置的当前天气数据和如何在API调用中使用API密钥 。
So we can change GetWeather
method like following to fix this error. 因此,我们可以像下面那样更改
GetWeather
方法来修复此错误。
public async static Task<RootObject> GetWeather(double lat, double lon)
{
var http = new HttpClient();
var response = await http.GetAsync($"http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={APIKEY}&units=metric");
var result = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<RootObject>(result);
return data;
}
Please note that I tried with your API key, but I got 401 Invalid API key
error, so please make sure you are using the right key. 请注意,我尝试使用您的API密钥,但收到
401 Invalid API key
错误,因此请确保您使用的密钥正确。 And also, we need to change Main.pressure
and Wind.deg
's type to double
as their value are not int
. 而且,我们还需要将
Main.pressure
和Wind.deg
的类型更改为double
因为它们的值不是int
。 If we don't change the type, we may get Newtonsoft.Json.JsonReaderException
while deserialization. 如果不更改类型,则反序列
Newtonsoft.Json.JsonReaderException
时可能会收到Newtonsoft.Json.JsonReaderException
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.