Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > static problem? Object reference not set to an instance of an object

Reply
Thread Tools

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

 
 
Michael Meckelein
Guest
Posts: n/a
 
      04-14-2004
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

 
Reply With Quote
 
 
 
 
Martin Dechev
Guest
Posts: n/a
 
      04-14-2004
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


 
Reply With Quote
 
 
 
 
Michael Meckelein
Guest
Posts: n/a
 
      04-14-2004
"Martin Dechev" <detcheff_@hotmail.com> 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

 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      04-14-2004
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$...
> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Object reference not set to an instance of an object" Weird thing happens with reference a link nguyentrongkha@gmail.com ASP .Net 1 09-20-2007 09:46 PM
Error:Object reference not set to an instance of an object. Suresh Kojhani ASP .Net 1 07-29-2004 12:10 PM
Error !Object reference not set to an instance of an object. !!! Help Parthiv Joshi ASP .Net 2 07-02-2004 10:28 AM
Object reference not set to an instance of an object. yysiow ASP .Net 1 07-12-2003 03:30 PM
Object reference not set to an instance of an object. Chris Fink ASP .Net 2 07-03-2003 06:48 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57