+ Reply to Thread
Results 1 to 6 of 6

Thread: Calling void isn't creating instance of an object

  1. #1
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Calling void isn't creating instance of an object

    I can't seem to work out why an instance of an object isn't getting created when I create it in a sub routine.

    In Page_Load if i have this:-
    Code:
    private Order order;
    protected void Page_Load()
    {
       if(!ispostback)
       {
          buildOrderItemList(ActionChoice.Build);
          order = new Order();
          order.orderItems = new List<Product>();
          order.id=-1;
          this.dlstOrderItems.DataSource=Order.orderItems;
          this.dlstOrderItems.DataBind();
       }
    }
    
    protected void buildOrderItemList(Action)
    {
       Order order = new Order();
       order.orderItems = new List<Product>();
    .
    .
    .
    }
    
    Without the lines in red, I get "instance of an object" error. If I create the instance in the page load I don't get the error.

    I use a similar technique with another Data List and it works fine.

    Can't work out why the instance isn't working in the void.

    Any ideas?

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    You have a class called "Order". This is a reference type. When you declare a variable of a reference type at run time, the variable contains the value null until you explicitly create an instance of the object by using the new operator, or assign it an object that has been created elsewhere by using new.

    On this page, you simply have:
    Code:
    private Order order;
    
    What you want is:
    Code:
    private Order order = new Order();
    
    OR, if you have a method, like GetOrder, for example, which returns an instance of the class, then you could have something like this:
    Code:
    private Order order = Orders.GetOrder(orderID);
    
    Make sense?
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  3. #3
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    That's what I have. The variable is used throughout the page hence the page wide declaration.

    If I create the instance directly in the Page_Load event, it works fine. If I try and create the instance inside a routine called from Page_Load, it doesn't seem to work and I get the "instance of an object" error.

    I can't seem to work out why that doesn't work.

    Ah, just noticed an error in the code I posted. The 2nd portion should have been:-
    Code:
    protected void buildOrderItemList(Action)
    {
       order = new Order();
       order.orderItems = new List<Product>();
    .
    .
    .
    }
    
    Actually, just realised that is the error in my code...

    It should have been like it is above. I had it as in my original post.

  4. #4
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Quote Originally Posted by richyrich View Post
    That's what I have.
    No, you don't. You have declared it as:
    Code:
    private Order order;
    
    This is NOT a new instance and will be null. Therefore, every method (i.e. page_load) that you use this in, you'll need to create an instance:
    Code:
    order = New Order();
    
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  5. #5
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Sorry, I didn't mean that's what I have. I knew what I meant...

    I meant I knew what you were talking about. I just declared another instance of it instead of referring to the page wide declared variable.

  6. #6
    Ask Me About Dragons :D Shadow Wizard has a spectacular aura about Shadow Wizard has a spectacular aura about Shadow Wizard's Avatar
    Join Date
    Jul 2008
    Location
    Israel
    Posts
    795
    Blog Entries
    2
    Real Name
    Yahav
    Rep Power
    5

    looks like common problem: re-declaring the global class variables inside
    local methods. I'm surprised Microsoft wasn't smart enough to block such
    behavior in compile time (in your case it would have thrown "variable name
    already in use" for the line Order order = new Order()) but maybe there's
    a reason behind this.
    anyway, put extra care to not "overwrite" global variables, as they become
    local and start as null from scratch.

+ Reply to Thread

Similar Threads

  1. Creating a drop down box from the css
    By dtz in forum HTML & CSS Help
    Replies: 1
    Last Post: March 12th, 2009, 09:10 AM
  2. Need help creating 3D array.
    By bryceowen in forum PHP Development
    Replies: 2
    Last Post: January 30th, 2009, 11:12 AM
  3. calling function
    By guddu in forum ASP Development
    Replies: 3
    Last Post: November 5th, 2008, 09:34 AM
  4. Object reference not set to an instance of an object
    By Shem in forum .NET Development
    Replies: 4
    Last Post: July 22nd, 2008, 05:59 AM
  5. Object reference not set to an instance of an object
    By jmurrayhead in forum .NET Development
    Replies: 1
    Last Post: May 29th, 2008, 11:16 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

SEO by vBSEO