Jump to content
  • Advertisement
Sign in to follow this  
PREDATOR_UK

Arrays and null objects...

This topic is 2382 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
}

Share this post


Link to post
Share on other sites
Advertisement

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])

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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];

Share this post


Link to post
Share on other sites
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();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!