473,395 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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

Nov 18 '05 #1
3 1353
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
Nov 18 '05 #2
"Martin Dechev" <de*******@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

Nov 18 '05 #3
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" <mi*****@go-on-line.de> wrote in message
news:c5************@ID-208606.news.uni-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

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
7
by: John A Grandy | last post by:
For a singleton class utilizes by ASP.NET 2.0 page processing: When initial instantiation is performed during the initial call to the retrieve instance method (let's call the method...
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
6
by: Grey Alien | last post by:
class A { public: A(const B& ref); private: static B& b ; }; How may b be initialized ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.