c# - Object not set to instance error with class invocation -


i set 3 models. i'm not sure if did best way, it's way understand easily. invoke departmentprofile class. first user found not manager goes else statement , fills in ad_userprofile , adds departmentprofile class.

the second user department manager goes if statement , errors out on first line object not set instance of object. missing? did not set models correctly?

when errors out

public class ad_userprofile     {         public string distinguishedname { get; set; }         public string email { get; set; }         public string manager { get; set; }         public string name { get; set; }         public string userprincipalname { get; set; } // useful if need allow user 'log on' different user.           public string username { get; set; }      }  public class ad_managerprofile     {         public string distinguishedname { get; set; }         public string email { get; set; }         public string manager { get; set; }         public string name { get; set; }         public string userprincipalname { get; set; } // useful if need allow user 'log on' different user.           public string username { get; set; }     }  public class ad_departmentprofile     {         public ad_departmentprofile()         {             this.ad_userprofile = new hashset<ad_userprofile>();         }          public string name { get; set; }          public virtual ad_managerprofile ad_managerprofile { get; set; }         public virtual icollection<ad_userprofile> ad_userprofile { get; set; }     } 

here invocation of classes:

public void getdepartmentinfo(string department, string owner = "jeremy")         {             directoryentry de = new directoryentry("ldap://server.server.com");              directorysearcher ds = new directorysearcher(de);              ds.filter = ("(&(objectcategory=person)(objectclass=user)(department=" + department + "))");             ds.searchscope = searchscope.subtree;              ad_departmentprofile dp = new ad_departmentprofile();              dp.name = department; // assign department name              foreach (searchresult temp in ds.findall())             {                 if (owner == temp.properties["samaccountname"][0].tostring())                 {                     //current user manager of department                      dp.ad_managerprofile.distinguishedname = temp.properties["distinguishedname"][0].tostring();  // line errors out instance not set object error.                     dp.ad_managerprofile.email = temp.properties["mail"][0].tostring();                     dp.ad_managerprofile.manager = temp.properties["manager"][0].tostring();                     dp.ad_managerprofile.name = temp.properties["name"][0].tostring();                     dp.ad_managerprofile.userprincipalname = temp.properties["userprincipalname"][0].tostring();                     dp.ad_managerprofile.username = temp.properties["samaccountname"][0].tostring();                 }                 else                 {                     //current user in department , not manage                      ad_userprofile p = new ad_userprofile();                      p.distinguishedname = temp.properties["distinguishedname"][0].tostring();                     p.email = temp.properties["mail"][0].tostring();                     p.manager = temp.properties["manager"][0].tostring();                     p.name = temp.properties["name"][0].tostring();                     p.userprincipalname = temp.properties["userprincipalname"][0].tostring();                     p.username = temp.properties["samaccountname"][0].tostring();                      dp.ad_userprofile.add(p);                 }             }         } 

i don't see anywhere you're initializing dp.ad_managerprofile, null. either give value in getdepartmentinfo or in constructor.

if (owner == temp.properties["samaccountname"][0].tostring()) {     //current user manager of department      dp.ad_managerprofile = new ad_managerprofile();      dp.ad_managerprofile.distinguishedname = temp.properties["distinguishedname"][0].tostring();  // line errors out instance not set object error. 

or

public ad_departmentprofile() {     this.ad_userprofile = new hashset<ad_userprofile>();     this.ad_managerprofile = new ad_managerprofile(); } 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

php - Dynamic url re-writing using htaccess -

java - Multi-Label Document Classification -