473,659 Members | 3,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem instantiating class field to null

I have defined to classes like this:

namespace PanelColors.Hel pers
{
public class PanelColor
{
public PanelColor()
{
r = 0; g = 0; b = 0;
}

public PanelColor(int red, int green, int blue)
{
r = red;
g = green;
b = blue;
}

public int R
{
get { return r; }
set { r = value; }
}

public int G
{
get { return g; }
set { g = value; }
}

public int B
{
get { return b; }
set { b = value; }
}

private int r, g, b;
}

public class PanelColorsArra y
{
public PanelColor[] panelColors; // Declare an array of PanelColor

public PanelColorsArra y()
{
panelColors = new PanelColor[] { cDay, cNight, cLuminous, cTemp };
panelColors.Ini tialize();
}

public void PanelColorsInit ()
{
cDay.R = 255; cDay.G = 255; cDay.B = 255;
cNight.R = 10; cNight.G = 10; cNight.B = 10;
cLuminous.R = 40; cLuminous.G = 220; cLuminous.B = 20;
cTemp.R = 0; cTemp.G = 0; cTemp.B = 0;
}

public PanelColor cDay;
public PanelColor cNight;
public PanelColor cLuminous;
public PanelColor cTemp;
} // class: PanelColorsArra y
} // namespace

In the code that uses them the cDay, cNight, cLuminous, and cTemp are NULL,
and do not point to fields within the class.

So, when I try to use them (and yes I know it is not encapsulated) like this
in the main code:

PanelColorsArra y ourColors = new PanelColorsArra y();
int curColor; // Point to day colors

ourColors.panel Colors[0].R = ....

I get an unhandled exception of type 'System.NullRef erenceException '
occurred in PanelColors.exe .

But I just don't get why when they are not getting instantiated by the
constructor.

Help.
Aug 2 '06 #1
2 1432
Stick <St***@discussi ons.microsoft.c omwrote:
I have defined to classes like this:

namespace PanelColors.Hel pers
{
public class PanelColor
public PanelColor cDay;
Should probably be:
public PanelColor cDay = new PanelColor();
// ... etc.
In the code that uses them the cDay, cNight, cLuminous, and cTemp are NULL,
and do not point to fields within the class.
Yes, that's the expected behaviour.
But I just don't get why when they are not getting instantiated by the
constructor.
Classes are never instantiated unless you instantiate them yourself,
with a 'new' expression. It's not like C++; all classes are always on
the heap, and must always be ultimately allocated via 'new'.

-- Barry

--
http://barrkel.blogspot.com/
Aug 2 '06 #2
I figured it out!

public PanelColor cDay;

needed to be:

public PanelColor cDay = new PanelColor(255, 255, 255);

Stick

"Stick" wrote:
I have defined to classes like this:

namespace PanelColors.Hel pers
{
public class PanelColor
{
public PanelColor()
{
r = 0; g = 0; b = 0;
}

public PanelColor(int red, int green, int blue)
{
r = red;
g = green;
b = blue;
}

public int R
{
get { return r; }
set { r = value; }
}

public int G
{
get { return g; }
set { g = value; }
}

public int B
{
get { return b; }
set { b = value; }
}

private int r, g, b;
}

public class PanelColorsArra y
{
public PanelColor[] panelColors; // Declare an array of PanelColor

public PanelColorsArra y()
{
panelColors = new PanelColor[] { cDay, cNight, cLuminous, cTemp };
panelColors.Ini tialize();
}

public void PanelColorsInit ()
{
cDay.R = 255; cDay.G = 255; cDay.B = 255;
cNight.R = 10; cNight.G = 10; cNight.B = 10;
cLuminous.R = 40; cLuminous.G = 220; cLuminous.B = 20;
cTemp.R = 0; cTemp.G = 0; cTemp.B = 0;
}

public PanelColor cDay;
public PanelColor cNight;
public PanelColor cLuminous;
public PanelColor cTemp;
} // class: PanelColorsArra y
} // namespace

In the code that uses them the cDay, cNight, cLuminous, and cTemp are NULL,
and do not point to fields within the class.

So, when I try to use them (and yes I know it is not encapsulated) like this
in the main code:

PanelColorsArra y ourColors = new PanelColorsArra y();
int curColor; // Point to day colors

ourColors.panel Colors[0].R = ....

I get an unhandled exception of type 'System.NullRef erenceException '
occurred in PanelColors.exe .

But I just don't get why when they are not getting instantiated by the
constructor.

Help.

Aug 2 '06 #3

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

Similar topics

1
1255
by: thechaosengine | last post by:
Hi everyone, I posted a question earlier about a collection class that I'd made seemingly getting instantiated magically after a class that contained a variable of that type had its constructor called. The constructor didnt touch the collection class and defineately didn't initialise it. Some one suggested the following reason as what is happening.
33
2831
by: abs | last post by:
Hi all. My list: <ul> <li id="a" onclick="show(this)">Aaaaaaaa</li> <li id="b" onclick="show(this)">Bbbbbbbb</li> <li id="c" onclick="show(this)">Cccccccc <ul> <li id="d" onclick="show(this)">111111</li>
6
2663
by: BBM | last post by:
I have an object that has a fairly complex construction sequence, so I have written a dedicated "factory" class that invokes the constructor of my object class (which does nothing but instantiate the object and set default blank/null values), and then does all the Db access and number crunching to populate the new object. The factory returns the fully populated object to the caller. All the fields in the object are private, but have...
10
4008
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the application. What should happen, is that the main MDI form should close, taking the child forms with it. There is code to loop through the child forms, remove the controls on each of them, and then close the form, but this code should execute only...
2
4443
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c */ #include <time.h>
5
4569
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public constructors (because we do not allow developers to instantiate them directly). Because our business objects are instantiated very frequently, the idea of using reflection sounds like a performance killer (I haven't done any tests on this, but the...
19
2512
by: Chocawok | last post by:
Some of the classes in my app are graphical. To encapsulate the graphical side of things I had created a class called "sprite" which holds a bit map and knows how to draw itself etc. The classes that are graphical contain a sprite object.
7
3451
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function 'UpdateRegistrant' expects parameter '@EMail', which was not supplied. The field value was null in the database and not changed in the FormView so is null going back into the stored procedure. I'm stumped and would greatly appreciate any suggestions.
3
6345
by: raylopez99 | last post by:
Below is my problem. I've narrowed it down to one thing: my unfamiliarity on how class instances are instantiated in an array. This is because the "un-array" / "non-array" version of the program works fine (see below). So what is the problem? I get a null reference on the line below at *!&!* "Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.? RL
0
8337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8531
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7359
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.