class - C# - Why am I getting a null reference exception? -


i confused why getting null reference exception when trying return dataset.

i have handler , dbaccess classes in ntier architecture.

first here portion on casefile handler/and access classes working fine. i'm including them because cant see why works , other doesn’t.

public class casefilehandler {     casefileaccess caseaccess = null;      public casefilehandler()     {         caseaccess = new casefileaccess();     }        public dataset getcasefiledataset(int caseid)     {         return caseaccess.getcasefiledataset(caseid);     }       public bool updatecasefile(casefile casefile)     {         return caseaccess.updatecasefile(casefile);     }      } 

and here access class. method returns dataset working fine.

public class casefileaccess {     const string connection_string = "data source = sqlserver;initial catalog=casemanager;integrated security=sspi;";      dbmanager _dal = new dbmanager(dataprovider.sqlserver, connection_string);       public casefile getcasefile(int caseid)     {         casefile casefile = new casefile();          _dal.open();         _dal.createparameters(1);         //stuff        return casefile;     }      public dataset getcasefiledataset(int caseid)     {         _dal.open();         _dal.createparameters(1);         _dal.addparameters(0, "@caseid", caseid);         dataset ds = _dal.executedataset(commandtype.storedprocedure, "sp_bl_adjustmentspage_read_case");         _dal.close();         return ds;     }      public bool updatecasefile(casefile casefile)     {         int result = 0;         _dal.createparameters(56);      //stuff          _dal.open();         result = _dal.executenonquery(commandtype.storedprocedure, "dbo.sp_bl_adjustmentspage_update_case");         _dal.close();         return (result > 0);     }  } 

and why confused. created handler , access class return datasets bind asp:dropdown lists , constructed classes in same way case file classes

here handler class:

public class ddmenuhandler {     ddmenuaccess menuaccess = null;      public ddmenuhandler()     {         ddmenuaccess menuaccess = new ddmenuaccess();     }      public dataset dscwkrcaseediting(int cwid)     {         return menuaccess.cwkrscaseediting(cwid);     }  } 

and access class:

 public class ddmenuaccess  {         const string connection_string = "data source = sqlserver;initial catalog=casemanager;integrated security=sspi;";          dbmanager _dal = new dbmanager(dataprovider.sqlserver, connection_string);          public dataset cwkrscaseediting(int cwid)         {             _dal.createparameters(1);             _dal.addparameters(0, "@id",cwid);             _dal.open();             dataset ds = _dal.executedataset(commandtype.storedprocedure, "sp_bl_cwkrsactivecaseedit");             _dal.close();             return ds;         }       } 

and code behind:

  protected global::adjustfiles.casefile casefile;   protected global::system.web.ui.webcontrols.dropdownlist ddcaseworker;  protected void page_load(object sender, eventargs e)     {         if (ispostback == false)         {             int caseid = convert.toint32(request.querystring["caseid"]);             mysession.current.editcaseid = caseid;              casefilehandler cfhandler = new casefilehandler();             casefile = cfhandler.getcasefile(caseid);              dataset dscase = cfhandler.getcasefiledataset(caseid);             fv_casefile.datasource = dscase;             fv_casefile.databind();             bindddcwkrs();          }     }       private void bindddcwkrs()     {         ddmenuhandler menuaccess = new ddmenuhandler();         dataset ds = menuaccess.dscwkrcaseediting(casefile.caseworkerid);         int = 0;         ddcaseworker.datasource = ds;         ddcaseworker.datatextfield = "cwname";         ddcaseworker.datavaluefield = "cwid";         ddcaseworker.databind();         ddcaseworker.selectedvalue = convert.tostring(casefile.caseworkerid);      } 

at line ddcaseworker.datasource = ds; getting null reference exception, menuaccess null. why?

source error:  line 29:public dataset dscwkrcaseediting(int cwid) line 30:{ line 31:     return menuaccess.cwkrscaseediting(cwid); line 32:} line 33:   system.nullreferenceexception unhandled user code message=object reference not set instance of object. source=adjustfiles stacktrace: @ adjustfiles.ddmenuhandler.dscwkrcaseediting(int32 cwid) in h:\visual studio 2010\projects\adjustfiles\adjustfiles\ddmenuhandler.cs:line 31        @ adjustfiles.adjustfile66editcase.bindddcwkrs() in h:\visual studio 2010\projects\adjustfiles\adjustfiles\adjustfile66editcase.aspx.cs:line 45        @ adjustfiles.adjustfile66editcase.page_load(object sender, eventargs e) in h:\visual studio 2010\projects\adjustfiles\adjustfiles\adjustfile66editcase.aspx.cs:line 29        @ system.web.util.callihelper.eventargfunctioncaller(intptr fp, object o, object t, eventargs e)        @ system.web.util.callieventhandlerdelegateproxy.callback(object sender, eventargs e)        @ system.web.ui.control.onload(eventargs e)        @ system.web.ui.control.loadrecursive()        @ system.web.ui.page.processrequestmain(boolean includestagesbeforeasyncpoint, boolean includestagesafterasyncpoint)   innerexception: 

you're instatiating ddmenuaccess local function variable instead of class variable:

the line:

ddmenuaccess menuaccess = new ddmenuaccess(); 

changed to:

this.menuaccess = new ddmenuaccess(); 

should work expected. ("this." optional, helps clarify)

public class ddmenuhandler {     ddmenuaccess menuaccess = null;      public ddmenuhandler()     {         this.menuaccess = new ddmenuaccess();     }      public dataset dscwkrcaseediting(int cwid)     {         return menuaccess.cwkrscaseediting(cwid);     } 

}


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -