Help To Solve Error

5 posts

Flag Post

Hi everyboby, i need some help solving this error

Error #1009: Cannot access a property or method of a null object reference.

I’m creating a platform game where the character can fire bullets, these bullets are created when the space bar is pressed and added to an array.

They are then controlled by my moveBullets function below. This is called in the enter frame handler.


private function moveBullets():void
		{
			for(var i:int = 0; i < _bullets.length; i++)
			{
				//Move bullet
				_bullets[i].x += _bullets[i].vx;
					
				//check stage boundaries for bullet
				if(_bullets[i].x + _bullets[i].width < 0
				||_bullets[i].x > _stage.stageWidth)
				{
					this.removeChild(_bullets[i]);
					_bullets[i] = null;
					_bullets.splice(_bullets[i], 1);
					i--;
				}
				
				//check collision with platforms
				if(_bullets[i] != null
				&& _bullets[i].hitTestObject(_platforms[1]))
				{
					this.removeChild(_bullets[i]);
					_bullets[i] = null;
					_bullets.splice(_bullets[i], 1);
					i--;
				}
			}
		}

The code works when only checking for the stage boudaries, but when I check for collisions with the platforms I get the #1009 error.

This error occurs when the bullet hits the platform and seems to be referring to this line of code


				//Move bullet
				_bullets[i].x += _bullets[i].vx;

Any ideas what I’m doing wrong and how to fix it?

Please let me know if I need to give more info or I havent been clear

Thanks

 
Flag Post

_bullets[i] is probably null

You’ll need to do a bit of debugging and code review to figure out why that is the case, perhaps first asserting my claim by tracing _bullets[i]

Perhaps you’ll want to look at the lines containing _bullets.splice(_bullets[i], 1);
Once you have determined the semantics bug, you will also need to understand the consequence of splicing from an array given the way you are iterating through your loop – you should not ignore this bug
EDIT: my mistake, sorry; read the rest of your code and you are indeed handling the iteration bug, albeit in an arguably unfavourable way

 
Flag Post

welcome on board.

That looks a bit messy.

I’d iterate backwards, so you do not have to readjsut the index, whenever you modify the array.
Setting the element to null before splicing it is not doing too much.

When you remove one element, use continue to start the iteration with the next (index wise: previous) element.

 
Flag Post
_bullets[i] = null;
_bullets.splice(_bullets[i], 1);

What is that splice call? Surely that should be _bullets.splice(i, 1). _bullets[i] will certainly be null at that point so you won’t be splicing correctly and that will get you next time you access that element.

 
Flag Post
Originally posted by BobJanova:
_bullets[i] = null;
bullets.splice(_bullets[i], 1);

What is that splice call? Surely that should be bullets.splice(i, 1). _bullets[i] will certainly be null at that point so you won’t be splicing correctly and that will get you next time you access that element.

Thanks! I knew there was a simple solution staring me in the face.

Thanks again