XNA Essentials

Game Programming for Xbox 360, PC + Windows Phone

NAVIGATION - SEARCH

Chapter 11

ok, Im working on creating the SimpleGame in chapter 11. Right now I just finished the EnemyManager.cs and put the appropriate code it my Game1.cs to make it all work, but when I compile and run it. The mainmenu starts and then I press enter to start the actual game but it pops up with this warning:


 


NullReferanceException was unhandled


Object reference not set to an instance of an object.


"i < enemies.Length;"


 


when refering to this part in my code:


        public override void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;


            for (int i=0; i < enemies.Length; i++)


            {
                Enemy enemy = enemiesIdea;
                if (enemy.Active)
                {
                    enemy.Position += (enemy.Velocity * elapsed);
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            for (int i = 0; i < enemies.Length; i++)
            {
                Enemy enemy = enemiesIdea;
                if (enemy.Active)
                {
                    cam.Draw(gameTime, "robot", spriteBatch, enemy.Position);
                }
            }

            base.Draw(gameTime);
        }


 


I have seriously tride everything... I dont know why it wont run that part of the code, the cd has the samethiing written and I cant find any other discrepancies in my code that would make it do something like this?

taterville - Tuesday, August 11, 2009 @ 10:42 PM

Re: Chapter 11

[quote user="taterville"]

NullReferanceException was unhandled


Object reference not set to an instance of an object.


"i < enemies.Length;"



[/quote]


 


taterville, welcome!


The error means that enemies is not being intialized.


Inside the Load method of the EnemyManager class you should have


enemies = new Enemy[maxEnemies];


If that is present, then the next thing to check is your code in your main Game class. In the ActivateGame method make sure you have the following condition:


if (!paused)


    enemies.Load(spriteBatch, 5, 20, 120.0f);


 


Hope that helps,


Chad

Chad Carter - Wednesday, August 12, 2009 @ 8:36 AM

Re: Chapter 11

unfortunately I've already got both of those... Thank you for the quick response, do you happen to have any more words of advice?  I even tride inputing the CD's code into the EnemyManager.cs and checked line by line to make sure that all my Game1.cs matched up but it still has the same error? This one is really throwing my for a loop.

taterville - Wednesday, August 12, 2009 @ 1:01 PM

Re: Chapter 11

I'm not at a place where I can easily bring up the source code to look, but to debug this, put a break point at both places I mentioned in the earlier reply -- Load method of the EnemyManager and ActivateGame method. You can then make sure that the code is actually being called before Update or Draw is called where it is trying to use the variable.


If recall, I believe the puased variable needs to be initialized to false. You may want to check that as well.


If I think of anything else, I'll post it up here. Maybe something else will jump out at me when I have the code in front of me.


Main thing to know is that the error is happening because the enemies variable is null - it is not being initialized. Now it is just about tracking down why it isn't being initialized.

Chad Carter - Wednesday, August 12, 2009 @ 3:02 PM

Re: Chapter 11

awesome! whew, thank you for the tips this one was really bugging me. I put in the break points and it would hit the if (!paused) state but wouldn't go inside and thats what was causing the error. so I fixed my paused statements and bingo!  Thanks again

taterville - Wednesday, August 12, 2009 @ 5:05 PM

Re: Chapter 11

Glad you got it working! You may want to check out the Advanced Debugging Tutorials. Setting break points is an awesome thing. Being able to control them in different ways (to only stop execution when a particular condition is true for example) can be really beneficial as well.


Good luck with the rest of the book!


Chad

Chad Carter - Wednesday, August 12, 2009 @ 11:27 PM

Re: Chapter 11

ok... Now I've got another problem I'm having with Chapter 11.  Im doing the Fadeout.cs game component now (page 245) and it tell us to use this code:


 


private SimpleGame simpleGame;


 


(then in the constructor)


simpleGame = (SimpleGame)game;


 


Im getting the error: 'SimpleGame' is a 'namespace' but is used like a 'type'


and:                            'SimpleGame' is a 'namespace' but is used like a 'variable'


is the code its telling me to use right?

taterville - Thursday, August 13, 2009 @ 9:39 PM

Re: Chapter 11

ok wooops nevermind... Sorry I spoke too soon. My Game1.cs class name was Game1 not SimpleGame like in the code.

taterville - Thursday, August 13, 2009 @ 9:45 PM

Re: Chapter 11

Glad you got it worked out!

Chad Carter - Thursday, August 13, 2009 @ 11:07 PM