Arrays and null objects...

Started by
6 comments, last by JonBonazza 12 years, 4 months ago

Any Help Appreciated - Thanks in advance.

ERROR MESSAGE:

Object reference not set to an instance of an object.

TRIGGER:

The code running the update map sequence.


LANGUAGE:

C#



Code:

switch (layer2[x, y])
{
case 1:
int health;
int level;
foreach (Power_Station pst in pstations)
{
if (pst.Getx() == x && pst.Gety() == y)
{
health = pst.GetHealth();
level = pst.GetLevel();
spriteBatch.Draw(power, texturerect, Color.White);
spriteBatch.DrawString(font1, health.ToString(), new Vector2(x, y + 20), Color.White);
}
}
break;
case 2:
spriteBatch.Draw(market, texturerect, Color.White);
break;
default:
break;
}
AMD Phenom II X6 1090T 3.2GHz
XFX ATI Radeon 5770 1GB GDDR5
ASUS M4A89GTD Pro USB 3.0
CORSAIR XMS3 4GB 1600MHz
THERMALTAKE V3
SEAGATE 500GB
WINDOWS 7 ULTIMATE 64 Bit
Advertisement
Which line of code, specifically, is halting with the error?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]


Which line of code, specifically, is halting with the error?


[color=#000088]if[color=#000000] [color=#666600]([color=#000000]pst[color=#666600].[color=#660066]Getx[color=#666600]()[color=#000000] [color=#666600]==[color=#000000] x [color=#666600]&&[color=#000000] pst[color=#666600].[color=#660066]Gety[color=#666600]()[color=#000000] [color=#666600]==[color=#000000] y[color=#666600])
AMD Phenom II X6 1090T 3.2GHz
XFX ATI Radeon 5770 1GB GDDR5
ASUS M4A89GTD Pro USB 3.0
CORSAIR XMS3 4GB 1600MHz
THERMALTAKE V3
SEAGATE 500GB
WINDOWS 7 ULTIMATE 64 Bit
What is the complete type of pstations?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Not enough code to say for sure, but I am going to go out on a whim and say you declared an array of objects, but never initialized each object.
Co-founder/Lead Programmer
Bonafide Software, L.L.C.
Fairmont, WV 26554 US
Sorry for the late reply...

I think it has something to do with how I am initializing the class array:

Class Code:

public class Power_Station
{
public int health = 100;
public int level = 0;
public int x = 0;
public int y = 0;

public void SetLevel(int lvl)
{
level = lvl;
}

public int GetHealth()
{
return health;
}

public int GetLevel()
{
return level;
}

public void setx(int newx)
{
x = newx;
}

public void sety(int newy)
{
y = newy;
}

public int Getx()
{
return x;
}

public int Gety()
{
return y;
}
}



Initialisation code:


Power_Station[] pstations;

pstations = new Power_Station[60];
AMD Phenom II X6 1090T 3.2GHz
XFX ATI Radeon 5770 1GB GDDR5
ASUS M4A89GTD Pro USB 3.0
CORSAIR XMS3 4GB 1600MHz
THERMALTAKE V3
SEAGATE 500GB
WINDOWS 7 ULTIMATE 64 Bit
Thanks for your help guys but I fixed it by using the following code in the initialisation method:

for (int i = 0; i!=60; i++)
{
pstations = new Power_Station();
}

AMD Phenom II X6 1090T 3.2GHz
XFX ATI Radeon 5770 1GB GDDR5
ASUS M4A89GTD Pro USB 3.0
CORSAIR XMS3 4GB 1600MHz
THERMALTAKE V3
SEAGATE 500GB
WINDOWS 7 ULTIMATE 64 Bit
Hehe, thought so. ;) Common mistake. Happens to everyone.
Co-founder/Lead Programmer
Bonafide Software, L.L.C.
Fairmont, WV 26554 US

This topic is closed to new replies.

Advertisement