-1

i am unable to add to the observablecollection list. variables are retrieved and are also stored in the class instance but i am not able to add that instance to the list

Nullreference exception found at the line : await meth(); in LoadedData method

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using News_Specific.Resources;
using System.ComponentModel;

using System.Collections.ObjectModel;
//using System.ComponentModel;
//using News_Specific.Resources;
namespace News_Specific.ViewModels
{
public class Search
{
    IndRes singleres = new IndRes();
   IndResGrp singleresgrp = new IndResGrp();
   //ObservableCollection<List<IndRes>> lst = new ObservableCollection<List<IndRes>>();
    //public Result res;
    public IndRes getresultitems{ get; set; }

    public bool IsDataLoaded { get; set; }

    public async void LoadData()
    {
        await meth();
    }


    public async Task<List<IndRes>> meth()
    {


        HttpClient client = new HttpClient();
        string key = "s8w8MsPnqPUpcBHCn6ok0evbZGI_";
        string topic = "windows";
        string baseUrl = "http://www.faroo.com/api?" +
                      "q={0}" +
                      "&start=1" +
                      "&key={1}" +
                      "&length=2" +
                      "&l=en" +
                      "&src=news" +
                      "&f=json";
        string url = string.Format(baseUrl,
                                   topic,
                                   key);

        string result = await client.GetStringAsync(url);



        RootObject obj = JsonConvert.DeserializeObject<RootObject>(result);
        if (obj.results != null)
        {
            foreach (Result res in obj.results)
            {
                singleres.title = res.title;
                singleres.kwic = res.kwic;
                singleresgrp.Items.Add(singleres);

            }

        }
        this.IsDataLoaded = true;
        return singleresgrp.Items;

    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }


}




}
smedury
  • 23
  • 2
  • Hey guys he is just a beginner just try to help him and when he got solve his ans then i would like the user to delete the questions by oneself. – loop Mar 26 '14 at 20:00

1 Answers1

0

You have to initialize you ingleresgrp.Items collection before adding data to it.for that do this in your meth() method.

singleresgrp.Items= new List<IndRes>();

You can add break points on the line where you trying to add items to your ingleresgrp.Items collection then you will find that it comes out to be null hence the exception.so just initialize it.

loop
  • 9,002
  • 10
  • 40
  • 76