static problem? Object reference not set to an instance of an object

Discussion in 'ASP .Net' started by Michael Meckelein, Apr 14, 2004.

  1. ASP.NET 1.1, c# web application

    I get an exception "Object reference not set to an instance of an object"
    for the follwoing line:
    myProductItem[0].ProductName = strProductName;

    I have a class for product information "productitem":

    public class productitem
    {
    public string ProductName;
    public double ProductPrice;
    public int ProductQuantity;
    public int ProductDiscount;

    public productitem()
    {
    //
    // TODO: Add constructor logic here
    //
    this.ProductName = "";
    this.ProductPrice = 0;
    this.ProductQuantity = 0;
    this.ProductDiscount = 0;
    }

    // I have declare an array of the productitem class as static:
    public static productitem[] myProductItem;

    // In my program I do the follwing
    if (myProductItem == null)
    {
    myProductItem = new productitem[1000];
    }

    myProductItem[0].ProductName = strProductName; // here the exception occur
    ....

    What's my fault?

    Thanks in advance,
    Michael
     
    Michael Meckelein, Apr 14, 2004
    #1
    1. Advertising

  2. Hi, Michael,

    This:

    > myProductItem = new productitem[1000];


    is equal to:

    myProductItem = new productitem[1000]
    {null,null,null,...};

    not to

    myProductItem = new productitem[1000]
    {new productitem(), new productitem(), ...};

    That's why an exception is thrown:

    > myProductItem[0].ProductName = strProductName; // here the exception occur


    at this point myProductItem[0] is null reference.

    Hope this helps
    Martin
     
    Martin Dechev, Apr 14, 2004
    #2
    1. Advertising

  3. "Martin Dechev" <> wrote in message
    > This:
    >
    > > myProductItem = new productitem[1000];

    >
    > is equal to:
    >
    > myProductItem = new productitem[1000]
    > {null,null,null,...};
    >
    > not to
    >
    > myProductItem = new productitem[1000]
    > {new productitem(), new productitem(), ...};
    >
    > That's why an exception is thrown:
    >
    > > myProductItem[0].ProductName = strProductName; // here the exception

    occur
    >
    > at this point myProductItem[0] is null reference.
    >


    That's right. Thank you Marin for your great assistance!

    Michael
     
    Michael Meckelein, Apr 14, 2004
    #3
  4. You created an array, but an array is just an array of nulls until you
    populate it, just as an array of strings has no strings in it until you
    assign a string to each element. In other words, taking the analogy of a
    variable as a box, you created 1000 empty boxes.

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    Big things are made up
    of lots of little things.

    "Michael Meckelein" <> wrote in message
    news:c5j0fl$26e1b$-berlin.de...
    > ASP.NET 1.1, c# web application
    >
    > I get an exception "Object reference not set to an instance of an object"
    > for the follwoing line:
    > myProductItem[0].ProductName = strProductName;
    >
    > I have a class for product information "productitem":
    >
    > public class productitem
    > {
    > public string ProductName;
    > public double ProductPrice;
    > public int ProductQuantity;
    > public int ProductDiscount;
    >
    > public productitem()
    > {
    > //
    > // TODO: Add constructor logic here
    > //
    > this.ProductName = "";
    > this.ProductPrice = 0;
    > this.ProductQuantity = 0;
    > this.ProductDiscount = 0;
    > }
    >
    > // I have declare an array of the productitem class as static:
    > public static productitem[] myProductItem;
    >
    > // In my program I do the follwing
    > if (myProductItem == null)
    > {
    > myProductItem = new productitem[1000];
    > }
    >
    > myProductItem[0].ProductName = strProductName; // here the exception occur
    > ...
    >
    > What's my fault?
    >
    > Thanks in advance,
    > Michael
    >
     
    Kevin Spencer, Apr 14, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. prince -=nore=-
    Replies:
    2
    Views:
    539
    prince -=nore=-
    Jan 3, 2005
  2. Chris
    Replies:
    35
    Views:
    1,377
    Scott M.
    Mar 11, 2007
  3. Replies:
    1
    Views:
    482
    Mark Rae [MVP]
    Sep 20, 2007
  4. David Bowey
    Replies:
    1
    Views:
    248
    David Bowey
    Mar 16, 2005
  5. David Bowey
    Replies:
    1
    Views:
    335
    David Bowey
    Mar 16, 2005
Loading...

Share This Page