c# - Why do i have this error NullReferenceException -


why have error on fundlist.children.add(fund) ?

an exception of type 'system.nullreferenceexception' occurred in icrc.hrssd.bal.dll not handled in user code

my model "tasktree" class there id, name , children, children list of other tasktree... need because have tree ...

    public list<tasktree> gettasktree(int idteam)     {         using (hrssd_data context = new hrssd_data())         {          list<tasktree> listall = new list<tasktree>();              var person = context.ar_person.where(a => a.status == "a" && a.cr_group.ref==idteam).tolist();             foreach (var item in person)             {                 tasktree fundlist = new tasktree();                 fundlist.id = item.ref;                 fundlist.name = item.full_name;                  var task = context.task.where(a => a.idcurrentofficer == item.ref && a.base_entity_type == 100 && a.open_ind == 1 && a.active_task == 1 && a.template_ind == 0&& a.task_status_ref != 114).tolist();                 foreach (var tasks in task)                 {                     tasktree fund = new tasktree();                     fund.name = tasks.task_title;                     fund.id = tasks.task_no;                      fundlist.children.add(fund);                  }                  listall.add(fundlist);             }             return listall;         }     } 

and tasktree class

public class tasktree {     public int id;     public string name;     public list<tasktree> children; } 

please try below code snippet.

public list<tasktree> gettasktree(int idteam) {     using (hrssd_data context = new hrssd_data())     {         list<tasktree> listall = new list<tasktree>();          var person = context.ar_person.where(a => a.status == "a" && a.cr_group.ref == idteam).tolist();         if (person != null)         {             foreach (var item in person)             {                 tasktree fundlist = new tasktree();                 fundlist.id = item.ref;                 fundlist.name = item.full_name;                  var task = context.task.where(a => a.idcurrentofficer == item.ref && a.base_entity_type == 100 && a.open_ind == 1 && a.active_task == 1 && a.template_ind == 0 && a.task_status_ref != 114).tolist();                  if (task != null)                 {                     // please check below line of code                      fundlist.children = new list<tasktree>();                      foreach (var tasks in task)                     {                         tasktree fund = new tasktree();                         fund.name = tasks.task_title;                         fund.id = tasks.task_no;                          fundlist.children.add(fund);                      }                 }                 listall.add(fundlist);             }         }         return listall;     } } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -