Jump to content

View more

Image of the Day

Inventory ! Va falloir trouver une autre couleur pour le cadre D: #AzTroScreenshot #screenshotsaturday https://t.co/PvxhGL7cOH
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Arrays and null objects...

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
7 replies to this topic

#1 PREDATOR_UK   Members   

102
Like
0Likes
Like

Posted 13 December 2011 - 03:17 PM

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

#2 ApochPiQ   Moderators   

22551
Like
0Likes
Like

Posted 13 December 2011 - 03:50 PM

Which line of code, specifically, is halting with the error?
Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

#3 PREDATOR_UK   Members   

102
Like
0Likes
Like

Posted 13 December 2011 - 03:52 PM

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


if (pst.Getx() == x && pst.Gety() == y)
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

#4 ApochPiQ   Moderators   

22551
Like
0Likes
Like

Posted 13 December 2011 - 07:47 PM

What is the complete type of pstations?
Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

#5 jonbonazza   Members   

121
Like
1Likes
Like

Posted 13 December 2011 - 08:19 PM

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

#6 PREDATOR_UK   Members   

102
Like
0Likes
Like

Posted 14 December 2011 - 11:22 AM

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

#7 PREDATOR_UK   Members   

102
Like
0Likes
Like

Posted 14 December 2011 - 11:56 AM

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[i] = 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

#8 jonbonazza   Members   

121
Like
0Likes
Like

Posted 14 December 2011 - 04:53 PM

Hehe, thought so. ;) Common mistake. Happens to everyone.
Co-founder/Lead Programmer
Bonafide Software, L.L.C.
Fairmont, WV 26554 US




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.