2014-03-26 8 views
-1

observablecollection 목록에 추가 할 수 없습니다. 변수가 검색되어 클래스 인스턴스에도 저장되지만 해당 인스턴스를 목록에 추가 할 수 없습니다.null 참조 예외가 발생했습니다 ... 도와주세요.

Nullreference 예외가 다음 줄에서 발견되었습니다. await meth(); LoadedData 방법에 은

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)); 
     } 
    } 


} 




} 
+0

안녕하세요, 그는 그냥 초보자이며 그를 도우려고 노력하고 있습니다. 그러면 자신의 ans를 해결 한 후 사용자가 직접 질문을 삭제하길 바랍니다. – loop

답변

0

당신은 당신의 메타() 메소드에서이 작업을 수행 되셨나요 데이터를 추가하기 전에 당신에게 ingleresgrp.Items 콜렉션을 초기화해야합니다.

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

당신은 당신이 당신이 그것을 따라서 exception.so 그냥 초기화 널 (null)로 나오는 것을 발견 할 것이다 당신의 ingleresgrp.Items 컬렉션에 항목을 추가하려고 라인에 브레이크 포인트를 추가 할 수 있습니다.

관련 문제