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

Problem instantiating class field to null

I have defined to classes like this:

namespace PanelColors.Helpers
{
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 PanelColorsArray
{
public PanelColor[] panelColors; // Declare an array of PanelColor

public PanelColorsArray()
{
panelColors = new PanelColor[] { cDay, cNight, cLuminous, cTemp };
panelColors.Initialize();
}

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: PanelColorsArray
} // 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:

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

ourColors.panelColors[0].R = ....

I get an unhandled exception of type 'System.NullReferenceException'
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 1422
Stick <St***@discussions.microsoft.comwrote:
I have defined to classes like this:

namespace PanelColors.Helpers
{
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.Helpers
{
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 PanelColorsArray
{
public PanelColor[] panelColors; // Declare an array of PanelColor

public PanelColorsArray()
{
panelColors = new PanelColor[] { cDay, cNight, cLuminous, cTemp };
panelColors.Initialize();
}

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: PanelColorsArray
} // 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:

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

ourColors.panelColors[0].R = ....

I get an unhandled exception of type 'System.NullReferenceException'
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
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...
33
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"...
6
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...
10
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...
2
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...
5
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...
19
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...
7
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...
3
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...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...

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.