CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2010
    Posts
    1

    Object created in one function, is null in an other function

    Hey,

    I would like to use the same Object in two functions. But somehow this object is null in the second function.

    You first have to click on btnOrder and than you can see the btnConfirm. Because this btnConfirm is on a other panel, which is invisible at start.


    Here's my code


    private OrderBO order;
    protected void btnOrder_Click(object sender, EventArgs e)
    {
    order= new OrderBO();
    order.Adress = txtAdress.Text;
    order.BookID = bookID;
    order.City = txtWoonplaats.Text;
    order.Count = Int32.Parse(txtCount.Text);
    order.Date = DateTime.Now;
    order.Email = txtEmail.Text;
    order.Name = txtNaam.Text;
    order.Pc = Int32.Parse(TxtPostcode.Text);

    panelOrder.Visible = false;
    panelConfirm.Visible = true;
    }

    protected void btnConfirm_Click(object sender, EventArgs e)
    {
    lblErrorInsert.Text = OrdersDAL.InsertOrder(order).ToString(); //Here is order null, why?
    lblErrorInsert.Visible = true;
    }


    In the function btnBevestig_Click is the Object order null, I don't see why?

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Object created in one function, is null in an other function

    Is there perhaps some other code that could be responsible which you have not shown here?
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Apr 2010
    Posts
    131

    Re: Object created in one function, is null in an other function

    For one thing, the global order is called "order," and in the confirm click, you are referencing ordersDAL, which I don't see ever being created in the code you posted....

    Looking at this, I would guess that odersDAL is throwing the null reference exception, not order.

    You can test by moving your instantiation outside of your method:

    private OrderBO order = new OrderBO();

    if you still get the null reference exception, you are not dealing with a null order.
    Last edited by mrgr8avill; May 23rd, 2010 at 11:33 AM.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Object created in one function, is null in an other function

    Quote Originally Posted by Vinzcent View Post
    In the function btnBevestig_Click is the Object order null, I don't see why?
    I don't see why either, but it would help if you posted the code for the btnBevestig_Click method. Also, is this method called after the btnOrder_Click method?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured