C# &引用;对象引用未设置为对象的实例;尝试访问类嵌套列表时

C# &引用;对象引用未设置为对象的实例;尝试访问类嵌套列表时,c#,asynchronous,uwp,nullreferenceexception,nested-lists,C#,Asynchronous,Uwp,Nullreferenceexception,Nested Lists,我正在写一个C#UWP程序来跟踪天气数据。尝试访问行Debug.WriteLine(“WEATHERPAGE中的调试:+myForecast.mysortedDays[1][1].desc”)在WeatherPage类中,我得到“对象引用未设置为对象的实例”sortedDays是一个嵌套列表,我需要能够完全访问它。我知道在我的GetWeather方法中sortedDays正在填充,但从另一个类引用时为空。我觉得这将是一个调用setter方法的例子,但我不能用链表成功地实现这一点 namespac

我正在写一个C#UWP程序来跟踪天气数据。尝试访问行
Debug.WriteLine(“WEATHERPAGE中的调试:+myForecast.mysortedDays[1][1].desc”)在WeatherPage类中,我得到“对象引用未设置为对象的实例”
sortedDays
是一个嵌套列表,我需要能够完全访问它。我知道在我的
GetWeather
方法中
sortedDays
正在填充,但从另一个类引用时为空。我觉得这将是一个调用setter方法的例子,但我不能用链表成功地实现这一点

namespace WeatherForecast
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class WeatherPage : Page
{
    Forecast myForecast;
    public WeatherPage()
    {
        this.InitializeComponent();

         myForecast = new Forecast();
        // Task task =myForecast.GetWeather("id=2964179");

        myForecast.GetWeather("id=2964179");

        Debug.WriteLine("DEBUG IN WEATHERPAGE: " + myForecast.sortedDays[1][1].desc);
    }
}
}


namespace WeatherForecast
{
class Forecast
{
    public List<List<WeatherController>> sortedDays { get; set; }
    public RootObject result { get; set; }

    public Forecast()
    {

    }
 public async void GetWeather(string cCode)
    {
        // DEBUG
        Debug.WriteLine("DEBUG: Started getWeather");
        string cityCode = cCode;
        string apiKey = "myapikey";
        // string cityCode = "id=2964179";
        string url = "myurl" + cityCode + apiKey;


        var uri = new Uri(url);
        using (HttpClient client = new HttpClient())
        {
            using (HttpResponseMessage response = await client.GetAsync(uri))
            {
                using (IHttpContent content = response.Content)
                {
                    var json = await content.ReadAsStringAsync();

                    result = JsonConvert.DeserializeObject<RootObject>(json);
                    //  SortWeather();

                    // create a list of weatherController lists to hold each day
                    // made public for global access
                    List<List<WeatherController>> sortedDays = new List<List<WeatherController>>();
                    //    sortedDays = new List<List<WeatherController>>();

                    //create a list of weatherController objects to hold each hourly interval
                    List<WeatherController> sortedHours = new List<WeatherController>();

                    // a base time
                    DateTime prevDate = Convert.ToDateTime("2000-01-01");
                    int counter = 0;

                    // iterate through result list  
                    for (int i = 0; i < result.list.Count(); i++)
                    {
                        // if the date is greater than the previous date add the sortedHours to sortedDays
                        if (Convert.ToDateTime(result.list[counter].dt_txt).Date > prevDate.Date && counter != 0)
                        {
                            sortedDays.Add(sortedHours);
                            sortedHours = new List<WeatherController>();
                        }
                        WeatherController wController = new WeatherController
                        {
                            dtime = result.list[counter].dt_txt,
                            dayOfWeek = (Convert.ToDateTime(result.list[counter].dt_txt).DayOfWeek).ToString(),
                            temp = result.list[counter].main.temp,
                            humidity = result.list[counter].main.humidity,
                            desc = result.list[counter].weather[0].description,
                            windSpeed = result.list[counter].wind.speed
                        };
                        sortedHours.Add(wController);

                        prevDate = Convert.ToDateTime(result.list[counter].dt_txt);
                        counter++;

                    }
                    // add any left over sortedHours to sortedDays
                    if (sortedHours != null)
                    {
                        sortedDays.Add(sortedHours);
                    }

                    Debug.WriteLine("DEBUG: Finished getWeather");

                }
            }
        }
    }


}
}
天气预报
{
/// 
///可以单独使用或在框架内导航到的空页。
/// 
公共密封部分类天气页面:第页
{
预测我的预测;
公共天气网页(
{
this.InitializeComponent();
myForecast=新预测();
//任务Task=myForecast.GetWeather(“id=2964179”);
myForecast.GetWeather(“id=2964179”);
Debug.WriteLine(“WEATHERPAGE中的调试:+myForecast.sortedDays[1][1].desc”);
}
}
}
天气预报
{
班级预测
{
公共列表排序日期{get;set;}
公共根对象结果{get;set;}
公众预测()
{
}
公共异步void GetWeather(字符串cCode)
{
//调试
WriteLine(“Debug:startedgetweather”);
字符串cityCode=cCode;
字符串apiKey=“myapikey”;
//字符串cityCode=“id=2964179”;
字符串url=“myurl”+cityCode+apiKey;
var uri=新的uri(url);
使用(HttpClient=new HttpClient())
{
使用(httpresponsemessageresponse=wait client.GetAsync(uri))
{
使用(IHttpContent content=response.content)
{
var json=await content.ReadAsStringAsync();
结果=JsonConvert.DeserializeObject(json);
//SortWeather();
//创建每天要保存的weatherController列表
//公开供全球访问
List sortedDays=新列表();
//sortedDays=新列表();
//创建weatherController对象列表,以保持每小时一次的间隔
List sortedHours=新列表();
//基准时间
DateTime prevDate=转换为ToDateTime(“2000-01-01”);
int计数器=0;
//遍历结果列表
for(int i=0;iprevDate.Date&&counter!=0)
{
sortedDays.添加(sortedHours);
sortedHours=新列表();
}
WeatherController wController=新的WeatherController
{
dtime=result.list[counter].dt_txt,
dayOfWeek=(Convert.ToDateTime(result.list[counter].dt_txt).dayOfWeek.ToString(),
temp=result.list[counter].main.temp,
湿度=结果.列表[计数器].main.湿度,
desc=result.list[counter]。天气[0]。说明,
风速=结果。列表[计数器]。风速
};
分拣时间。添加(wController);
prevDate=Convert.ToDateTime(result.list[counter].dt_txt);
计数器++;
}
//将剩余的分拣时间添加到分拣日期
if(sortedHours!=null)
{
sortedDays.添加(sortedHours);
}
Debug.WriteLine(“Debug:Finished getWeather”);
}
}
}
}
}
}

您正在分配具有相同名称的局部变量。见A点 所以基本上你永远不会分配sortedDays的Forecast属性。您必须执行第B点中的任一代码 或添加以下行:

this.sortedDays=sortedDays

namespace WeatherForecast
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class WeatherPage : Page
    {
        Forecast myForecast;
        public WeatherPage()
        {
            this.InitializeComponent();

            myForecast = new Forecast();
            // Task task =myForecast.GetWeather("id=2964179");
            // POINT C
            myForecast.GetWeather("id=2964179").GetAwaiter().GetResult();
            //POINT D
            Debug.WriteLine("DEBUG IN WEATHERPAGE: " + myForecast.sortedDays[1][1].desc);
        }
    }
}

namespace WeatherForecast
{
    class Forecast
    {
        public List<List<WeatherController>> sortedDays { get; set; }
        public RootObject result { get; set; }

        public Forecast()
        {

        }

        public async Task GetWeather(string cCode)
        {
            // DEBUG
            Debug.WriteLine("DEBUG: Started getWeather");
            string cityCode = cCode;
            string apiKey = "myapikey";
            // string cityCode = "id=2964179";
            string url = "myurl" + cityCode + apiKey;


            var uri = new Uri(url);
            using (HttpClient client = new HttpClient())
            {
                using (HttpResponseMessage response = await client.GetAsync(uri))
                {
                    using (IHttpContent content = response.Content)
                    {
                        var json = await content.ReadAsStringAsync();

                        result = JsonConvert.DeserializeObject<RootObject>(json);
                        //  SortWeather();

                        // create a list of weatherController lists to hold each day
                        // made public for global access
                        // POINT A
                        List<List<WeatherController>> sortedDays = new List<List<WeatherController>>(); 

                        // POINT B
                        //    sortedDays = new List<List<WeatherController>>();

                        //create a list of weatherController objects to hold each hourly interval
                        List<WeatherController> sortedHours = new List<WeatherController>();

                        // a base time
                        DateTime prevDate = Convert.ToDateTime("2000-01-01");
                        int counter = 0;

                        // iterate through result list  
                        for (int i = 0; i < result.list.Count(); i++)
                        {
                            // if the date is greater than the previous date add the sortedHours to sortedDays
                            if (Convert.ToDateTime(result.list[counter].dt_txt).Date > prevDate.Date && counter != 0)
                            {
                                sortedDays.Add(sortedHours);
                                sortedHours = new List<WeatherController>();
                            }
                            WeatherController wController = new WeatherController
                            {
                                dtime = result.list[counter].dt_txt,
                                dayOfWeek = (Convert.ToDateTime(result.list[counter].dt_txt).DayOfWeek).ToString(),
                                temp = result.list[counter].main.temp,
                                humidity = result.list[counter].main.humidity,
                                desc = result.list[counter].weather[0].description,
                                windSpeed = result.list[counter].wind.speed
                            };
                            sortedHours.Add(wController);

                            prevDate = Convert.ToDateTime(result.list[counter].dt_txt);
                            counter++;

                        }
                        // add any left over sortedHours to sortedDays
                        if (sortedHours != null)
                        {
                            sortedDays.Add(sortedHours);
                        }

                        Debug.WriteLine("DEBUG: Finished getWeather");

                    }
                }
            }
        }
    }
}
天气预报
{
/// 
///可以单独使用或在框架内导航到的空页。
/// 
公共密封部分类天气页面:第页
{
预测我的预测;
公共天气网页(
{
this.InitializeComponent();
myForecast=新预测();
//任务Task=myForecast.GetWeather(“id=2964179”);
//C点
myForecast.GetWeather(“id=2964179”).GetAwaiter().GetResult();
//D点
Debug.WriteLine(“WEATHERPAGE中的调试:+myForecast.sortedDays[1][1].desc”);
}
}
}
天气预报
{
班级预测
{
公共列表排序日期{get;set;}
公共根对象结果{get;set;}
公众预测()
{
}
公共异步任务GetWeather(字符串cCode)
{
//调试
WriteLine(“Debug:startedgetweather”);
字符串cityCode=cCode;
字符串apiKey=“myapikey”;
//字符串cityCode=“id=2964179”;
字符串url=“myurl”+cityCode+apiKey;
var uri=新的uri(url);
使用(HttpClient=new HttpClient())
{
使用(httpresponsemessageresponse=wait client.GetAsync(uri))
{
使用(IHttpContent content=response.content)
{