简体   繁体   English

UWP-Weather-App.exe中发生类型为'System.NullReferenceException'的异常,但未在用户代码中处理

[英]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.pressureWind.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.

相关问题 myproject.DLL中发生了'System.NullReferenceException'类型的异常,但未在用户代码中处理 - An exception of type 'System.NullReferenceException' occurred in myproject.DLL but was not handled in user code EntityFramework.dll中发生类型&#39;System.NullReferenceException&#39;的异常,但未在用户代码中处理:执行存储过程 - An exception of type 'System.NullReferenceException' occurred in EntityFramework.dll but was not handled in user code : Executing a stored procedure 错误:exe中发生类型&#39;System.NullReferenceException&#39;的异常 - Error: exception of type 'System.NullReferenceException' occurred in exe WindowsFormsApplication1.exe中发生了类型为&#39;System.NullReferenceException&#39;的未处理异常 - An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication1.exe mscaptcha.dll中发生类型为&#39;System.NullReferenceException&#39;的异常,但未在用户代码中处理 - An exception of type 'System.NullReferenceException' occured in mscaptcha.dll but was not handled in user code App_Web _ ***。dll中出现“System.NullReferenceException”类型的异常 - An exception of type 'System.NullReferenceException' occurred in App_Web_***.dll 发生类型为“ System.NullReferenceException”的异常 - An exception of type 'System.NullReferenceException' occurred 我该如何解决此错误:DotTeach.exe中发生了&#39;System.NullReferenceException&#39;类型的未处理异常 - how do i fix this error :An unhandled exception of type 'System.NullReferenceException' occurred in DotTeach.exe 创建文本框表中出现“System.NullReferenceException”类型的异常 - An exception of type 'System.NullReferenceException' occurred in create textboxes table Windows Phone 8应用程序,NavigationService期间发生了类型为&#39;System.NullReferenceException&#39;的异常 - Windows phone 8 app, An exception of type 'System.NullReferenceException' occurred during NavigationService
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM