473,387 Members | 1,540 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,387 software developers and data experts.

OBJECT REFERENCE NOT SET TO AN INSTANCE OF AN OBJECT. but the code is correct

here Box is an user defined class
Box[] b;
b = new Box[100];
//b = new Box[10, 10];
/*b = new Box[10][];
for (int i = 0; i < 10; i++)
{
b[i] = new Box[10];
}*/
if(size==0)
size = 4;// defaullt size
for (int i = 0; i <size; i++)
{
for (int j = 0; j < size; j++)
{
if (i == 0)
b[i * size + j].N = false;// "OBJECT REFERENCE
NOT SET TO AN INSTANCE OF AN OBJECT."
if (i == size - 1)
b[i * size + j].S = false;
if (j == 0)
b[i * size + j].W = false;
if (j == size - 1)
b[i * size + j].E = false;
}
}

why is that error coming..
i had declared the variable and also allocated memory using the new
keyword..

It is saying to "USE new KEYWORD TO CREATE AN OBJECT INSTANCE"

Sep 23 '07 #1
3 1248
sravan_reddy001 wrote:
[...]
why is that error coming..
i had declared the variable and also allocated memory using the new
keyword..
Yes, but you never initialized the allocated memory to anything useful.

This sort of search is useful for these kinds of questions:
http://groups.google.com/group/micro...T+SET%22+array

Here's a result from the above search that is pretty much identical to
the problem you're having:
http://groups.google.com/group/micro...7d26b8ad1ccc40

Pete
Sep 23 '07 #2
i had declared the variable and also allocated memory using the new
keyword..
No; the code never creates any Box instances; it is commented out (and
uses jagged-array syntax); arrays of reference-types (classes)
initialise all values to null; you need something like:

for (int i = 0; i < 100; i++) {
b[i] = new Box();
}

Marc

Sep 23 '07 #3
thanks a lot..

its a bit of confusion....

Sep 27 '07 #4

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

Similar topics

6
by: lawrence | last post by:
How dangerous or stupid is it for an object to have a reference to the object which contains it? If I have a class called $controllerForAll which has an arrray of all the objects that exist, what...
2
by: Pkpatel | last post by:
Hi, I keep getting this error every time I try to load crystalreportviewer on a webform with a dataset. Here is the error: -------------------------------------------------------- Server...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
7
by: Brett | last post by:
I'm not sure why I keep getting this error, "Object reference not set to an instance of an object". Private Function somefunction() as string Dim MyCurrentClass As New Class1 Try For i As...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
1
by: vishnu | last post by:
Hi, I am working on asp.net project which I converted the code fron VB to C# and instead of RaiseEvent in VB code I used the following code. using System; using System.Data; using...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.