-2

I am confused as to why I am getting a null reference exception when trying to return a Dataset.

I have a handler and DBaccess classes as in an nTier architecture.

First here is a portion on my casefile handler/and access classes which are working fine. I'm including them because I cant see why this works and the 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 is the access class. The method that returns a dataset is working just 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 this is why I am confused. I created a handler and an access class to return datasets to bind to asp:dropdown lists and constructed the classes in the same way as the case file classes

Here is the handler class:

public class ddMenuHandler
{
    ddMenuAccess menuaccess = null;

    public ddMenuHandler()
    {
        ddMenuAccess menuaccess = new ddMenuAccess();
    }

    public DataSet dsCwkrCaseEditing(int cwID)
    {
        return menuaccess.CwkrsCaseEditing(cwID);
    }

}

And the 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 this is my 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 i = 0;
        ddCaseworker.DataSource = ds;
        ddCaseworker.DataTextField = "CwName";
        ddCaseworker.DataValueField = "CwID";
        ddCaseworker.DataBind();
        ddCaseworker.SelectedValue = Convert.ToString(casefile.CaseWorkerID);

    }

At the line ddCaseworker.DataSource = ds; is where I am getting my null reference exception, menuaccess is null. But why?

Source Error:

Line 29:public DataSet dsCwkrCaseEditing(int cwID)
Line 30:{
Line 31:     return menuaccess.CwkrsCaseEditing(cwID);
Line 32:}
Line 33:


System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=Adjustfiles
StackTrace:
at Adjustfiles.ddMenuHandler.dsCwkrCaseEditing(Int32 cwID) in H:\Visual Studio 2010\Projects\Adjustfiles\Adjustfiles\ddMenuHandler.cs:line 31
       at Adjustfiles.adjustFile66EditCase.BindDdCwkrs() in H:\Visual Studio 2010\Projects\Adjustfiles\Adjustfiles\adjustFile66EditCase.aspx.cs:line 45
       at Adjustfiles.adjustFile66EditCase.Page_Load(Object sender, EventArgs e) in H:\Visual Studio 2010\Projects\Adjustfiles\Adjustfiles\adjustFile66EditCase.aspx.cs:line 29
       at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:
eightShirt
  • 1,387
  • 2
  • 13
  • 25
Sonny123
  • 107
  • 1
  • 3
  • 12
  • `ddCaseworker` must be null. Where is this being initialised? – ChrisF Apr 04 '13 at 11:39
  • 1
    see http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net for help on `NullReferenceException` – pascalhein Apr 04 '13 at 11:40
  • ddCaseworker is an asp dropdown, i am confused as to what you mean. i'll edit my code behind ddcaseworker is object on the page – Sonny123 Apr 04 '13 at 11:45

1 Answers1

6

You're instatiating ddMenuAccess as a local function variable instead of the class variable:

The line:

ddMenuAccess menuaccess = new ddMenuAccess();

Changed to:

this.menuaccess = new ddMenuAccess();

Should work as expected. ("this." optional, but helps clarify)

public class ddMenuHandler
{
    ddMenuAccess menuaccess = null;

    public ddMenuHandler()
    {
        this.menuaccess = new ddMenuAccess();
    }

    public DataSet dsCwkrCaseEditing(int cwID)
    {
        return menuaccess.CwkrsCaseEditing(cwID);
    }

}

vidario
  • 479
  • 2
  • 5
  • +1, exception is thrown at the `31` line, which is `menuaccess.CwkrsCaseEditing` statement. Strange why someone downvoted this – Ilya Ivanov Apr 04 '13 at 11:47
  • +1 from me too for getting it right (and reading all that code) – dash Apr 04 '13 at 11:48
  • oh yes I see. But why did work in the casefile handler class? – Sonny123 Apr 04 '13 at 11:49
  • 1
    @Sonny123 because in it you instantiate field variable `caseaccess = new CaseFileAccess();`, which is equivalent to `this.caseaccess = new CaseFileAccess();` – Ilya Ivanov Apr 04 '13 at 11:52
  • Doh!! Okay but im still getting a Null exceptionim just getting at At the line ddCaseworker.DataSource = ds; in the BindddCwkrs() method – Sonny123 Apr 04 '13 at 12:02
  • I think i must have typo'd or something when i was trying to do stuff. But i am now back where i started from with my original exception. At the line ddCaseworker.DataSource = ds – Sonny123 Apr 04 '13 at 12:06
  • If you "run to here" or otherwise debug, what exactly is null - ddCaseworker, ds or ddCaseworker.DataSource ? – vidario Apr 04 '13 at 12:48
  • okay i'm good, i introduced another null exception while I was playing around trying to get it work then got confused when i posted the original question but now im good – Sonny123 Apr 04 '13 at 13:12