00:00
00:00
Newgrounds Background Image Theme

BorfDoggo just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Error 1009 (tried everything) AS3

4,774 Views | 39 Replies
New Topic Respond to this Topic

Error 1009 (tried everything) AS3 2012-07-27 20:47:11


I've been working on this game (click to see) for awhile and, although it's doing what I want it to do so far, I'm still getting errors. The only specific error I'm getting is:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
	at GameDocumentClass/checkEnemyCollisions()
	at GameDocumentClass/enterFrameHandler()

The enemies disappear after their health is depleted and the bullets also disappear when they impact something, so I am not sure why I'm getting any error. Here is the code for the "checkEnemyCollisions" function:

public function checkEnemyCollisions() :void{
			var zeldaHit:ZeldaHit = new ZeldaHit();
			var soundChannel2:SoundChannel = new SoundChannel;
			var bullet:MovieClip;
			var enemy:MovieClip;
			var impact = new Impact();
  for (var i:int = 0; i < bulletArray.length; i++){
    for(var j:int = 0; j < enemyArray.length; j++){
		bullet = bulletArray[i];
		enemy = enemyArray[j];
      if (enemy.hitTestPoint(bullet.x, bullet.y, true)) {
 
					soundChannel2 = zeldaHit.play();
					stage.removeChild(bullet);
					stage.addChild(impact);
					impact.x = bullet.x;
					impact.y = bullet.y;
					enemy.enemyHealth --;
					enemy.gotoAndPlay("enemyHit");
					bulletArray.splice(i,1);

	  }
    }
  }
}

I've tried tracing every variable in this code and nothing is returned null. It also mentions my "enterFramHandler" function and here it is:

private function enterFrameHandler(event:Event):void
		{
			checkRupeeCollisions();
			checkEnemyCollisions();
			checkGroundCollisions();
			fire();
			
		}

Now, the checkRupeeCollisions function and checkEnemyCollisions function is exactly the same, with the exception of any "gotoAndPlay" function, since there is no hit animation for the rupees right now, but I do not get an error when I hit a rupee (even after trying adding a health value to them). I am guessing the problem may lie in my enemy class functions which is here:

package{
	import flash.display.MovieClip;
	import fl.transitions.*;
	import flash.events.Event;
	import flash.media.*
	import GameDocumentClass;
	public class Enemy extends MovieClip
	{
		
		private var speed = 1;
		public var enemyHealth:Number = 10;
		private var enemyTimer = 0;
		
		
		public function Enemy()
		{
			addEventListener(Event.ENTER_FRAME, updateEnemy);
			this.addEventListener(Event.ENTER_FRAME, removeEnemy);
		}
		
		private function updateEnemy(event:Event)
		{
			moveEnemy();
			if (enemyTimer >= 30){
				speed = speed * -1;
				enemyTimer = 0;
			}
		}
		
		private function moveEnemy():void
		{
				this.x -= speed;
				enemyTimer ++;
				
		}
		
		private function removeEnemy(e:Event)
		{
			var zeldaKill:ZeldaKill = new ZeldaKill();
			var soundChannel3:SoundChannel = new SoundChannel;
			if(enemyHealth <= 0){
			soundChannel3 = zeldaKill.play();
			trace(GameDocumentClass.enemyArray);
			stage.removeChild(this);
			removeEventListener(Event.ENTER_FRAME, removeEnemy);
			}
		}
	}
}

Does anyone see anything wrong with this? I know it's a lot to ask, but I would appreciate any try from anyone. Thanks!

Response to Error 1009 (tried everything) AS3 2012-07-27 21:04:52


At 7/27/12 08:47 PM, Barzona wrote: I've been working on this game (click to see) for awhile and, although it's doing what I want it to do so far, I'm still getting errors. The only specific error I'm getting is:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at GameDocumentClass/checkEnemyCollisions()
at GameDocumentClass/enterFrameHandler()

The enemies disappear after their health is depleted and the bullets also disappear when they impact something, so I am not sure why I'm getting any error. Here is the code for the "checkEnemyCollisions" function:

public function checkEnemyCollisions() :void{
var zeldaHit:ZeldaHit = new ZeldaHit();
var soundChannel2:SoundChannel = new SoundChannel;
var bullet:MovieClip;
var enemy:MovieClip;
var impact = new Impact();
for (var i:int = 0; i < bulletArray.length; i++){
for(var j:int = 0; j < enemyArray.length; j++){
bullet = bulletArray[i];
enemy = enemyArray[j];
if (enemy.hitTestPoint(bullet.x, bullet.y, true)) {

soundChannel2 = zeldaHit.play();
stage.removeChild(bullet);
stage.addChild(impact);
impact.x = bullet.x;
impact.y = bullet.y;
enemy.enemyHealth --;
enemy.gotoAndPlay("enemyHit");
bulletArray.splice(i,1);

}
}
}
}

I think your problem is caused by the line "bulletArray.splice(i,1);"

Lets say "i" equals 4, and your bullet array looks like this:
[b1,b2,b3,b4,b5]

i=4 points at the bullet "b5". Now you run the line "bulletArray.splice(i,1);" and your bullet array looks like this

[b1,b2,b3,b4]

Now i=4 doesn't point to anything, but the inner "j" loop continues and tries to use a bullet reference that doesn't point at anything.

I hope that makes sense to you, I'm not that great at explaining things : /.


You forgot to use code tags, didn't you?

Response to Error 1009 (tried everything) AS3 2012-07-27 21:20:31


ill help and explain it better XD

Your array has 5 elements which are counted like this.
0 1 2 3 4

in each element you have a bullet.

Your code goes to the 4th element which is your last of the array. Does something, removes an element. Now your array looks like this.
0 1 2 3

you try to access the 4th element again. But there is no element to access. ERROR

Response to Error 1009 (tried everything) AS3 2012-07-27 21:34:34


There's twoways to handle this.

1. return, simply run return after the bullet is removed end it will end the function, you may not be able to do this depending on how the function is set up.

2. Break outer loop, "break" only breaks out of the inner most loop if you use it by itself, you'll have to define your outer loop with a name and tell break to break it. Example:

outerLoop: for (var i:int = 0; i < 5; i++) {
    for (var j:int = 0; j < 5; j++) {
            break outerLoop;
    }
}

That is, if I understand the problem correctly, hope that helps.


If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to Error 1009 (tried everything) AS3 2012-07-27 21:51:07


At 7/27/12 09:20 PM, PMMurphy wrote: ill help and explain it better XD

Your array has 5 elements which are counted like this.
0 1 2 3 4

in each element you have a bullet.

Your code goes to the 4th element which is your last of the array. Does something, removes an element. Now your array looks like this.
0 1 2 3

I understand what you are saying..hmm. I tried reversing the collision check loops (putting the enemy array loop outside the bullet array loop), but that didn't fix any problems, really.

I guess I really need to think of a better way to test interactions between my bullets and my targets. I have a strong feeling that my current approach is so very wrong.


you try to access the 4th element again. But there is no element to access. ERROR

Response to Error 1009 (tried everything) AS3 2012-07-27 22:33:25


Oh, well I see I was ignored. . . :/


If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to Error 1009 (tried everything) AS3 2012-07-27 22:41:48


At 7/27/12 10:33 PM, MintPaw wrote: Oh, well I see I was ignored. . . :/

I never knew that you could name loops, so at least I learned something new today.

D

You forgot to use code tags, didn't you?

Response to Error 1009 (tried everything) AS3 2012-07-27 22:42:44


At 7/27/12 10:33 PM, MintPaw wrote: Oh, well I see I was ignored. . . :/

I'm sorry, I didn't even see your post. I'm going to give it a try and I will tell you if it works. Thanks!

It has just been very difficult to wrap my head around how these hittest loops are supposed to work.

Response to Error 1009 (tried everything) AS3 2012-07-27 22:45:03


I can't help with any coding or ways to solve it. But if you write things in pseudo code i can give suggestions.

Thats if you havn't found your answer.

Response to Error 1009 (tried everything) AS3 2012-07-27 22:47:31


At 7/27/12 10:45 PM, PMMurphy wrote: I can't help with any coding or ways to solve it. But if you write things in pseudo code i can give suggestions.

Thats if you havn't found your answer.

actually i am going to step in here and give a fair warning. By using the commands suggested to you, you are altering the flow of the language. These commands are meant to be used in assembly and assembly only. They CAN be used in actual languages but it changes how the language is constructed and made to operate. By programming like this you practice bad programming because languages are made to be run from top to bottom. A loop is meant to continue to exsist until it exits. By putting in breaks and other various commands you disrupt the natural flow of your program.

Response to Error 1009 (tried everything) AS3 2012-07-27 23:29:16


At 7/27/12 10:47 PM, PMMurphy wrote: actually i am going to step in here and give a fair warning. By using the commands suggested to you, you are altering the flow of the language. These commands are meant to be used in assembly and assembly only. They CAN be used in actual languages but it changes how the language is constructed and made to operate. By programming like this you practice bad programming because languages are made to be run from top to bottom. A loop is meant to continue to exsist until it exits. By putting in breaks and other various commands you disrupt the natural flow of your program.

What are you talking about, break and return are completely necessary keywords for general programming just as other keywords such as function, for, each, et cetera.


If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to Error 1009 (tried everything) AS3 2012-07-27 23:47:11


I think I have come to understand my problem with your help and have, so far, fixed it.

I decided to trace the length of my bullet array every time I fired a bullet. It was incrementing as expected, however, the length of the bullet array whenever the error would appear was the exact same as the last shot, so, for example the array was 14 on the last shot and 14 on the error shot. As it was splicing numbers from the array, it would, on occasion, have the same length for a different bullet.

When I removed the splice from the array, the error stopped happening. I also tried setting the splice to array.splice( i - 1 ); and that also worked.

Not sure if that's the best way to do this, but it's all I have so far.

Response to Error 1009 (tried everything) AS3 2012-07-27 23:52:17


What are you talking about, break and return are completely necessary keywords for general programming just as other keywords such as function, for, each, et cetera.

Break is assembly i am pretty sure, return is fine. I will double check.

Response to Error 1009 (tried everything) AS3 2012-07-27 23:55:43


What are you talking about, break and return are completely necessary keywords for general programming just as other keywords such as function, for, each, et cetera.

Ok i was mistaken. But it doesn't matter because your method of solving the problem doesn't solve the problem. If i did that on a programming assignment at my university i would get an F.

Response to Error 1009 (tried everything) AS3 2012-07-28 00:31:47


Then how would you pull this off?


If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to Error 1009 (tried everything) AS3 2012-07-28 00:38:38


Its an index out of bounds error. When you trim the array you decrease the index locator at the same time.

Response to Error 1009 (tried everything) AS3 2012-07-28 00:44:34


At 7/28/12 12:38 AM, PMMurphy wrote: Its an index out of bounds error. When you trim the array you decrease the index locator at the same time.

I guess that works, but I've never had a problem or ever been told not to use break, I'm not a super pro or anything, but I've been programming for a good five years, I've read books and stuff where they use break. :/


If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to Error 1009 (tried everything) AS3 2012-07-28 00:47:01


people use break often and people do it all the time.

But if your learning you need to do it without. Those people who use break either know what good code looks like or dont.

By solving your problems with break or return without knowing why your using them. Your simply just putting duct tape over your code. It may work, but it looks tacky and doesn't make any sense.

The only times its ok to use break or return is for the following reasons.

1) it significantly shortens the length of the code and is readable.
or
2) it is the only solution to the problem

Response to Error 1009 (tried everything) AS3 2012-07-28 01:04:32


unless you are returning variables and booleans. Thats a totally different story :)

Response to Error 1009 (tried everything) AS3 2012-07-28 02:07:52


At 7/28/12 12:47 AM, PMMurphy wrote: But if your learning you need to do it without. Those people who use break either know what good code looks like or dont.

break; was one of the first things I learned how to use while programming. When you have a loop searching through an array of 1 million elements, and you find the element you need at index 2, are you going to go through the rest of the 999, 998 elements? Seems pretty damn slow to me.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to Error 1009 (tried everything) AS3 2012-07-28 02:12:42


At 7/28/12 02:07 AM, egg82 wrote:
At 7/28/12 12:47 AM, PMMurphy wrote: But if your learning you need to do it without. Those people who use break either know what good code looks like or dont.
break; was one of the first things I learned how to use while programming. When you have a loop searching through an array of 1 million elements, and you find the element you need at index 2, are you going to go through the rest of the 999, 998 elements? Seems pretty damn slow to me.

Yea but your describing a proper way to use break. Look at the topic in this thread and see the example he used break in. I forgot to mention if it makes it more efficient.

Response to Error 1009 (tried everything) AS3 2012-07-28 02:15:31


At 7/28/12 02:07 AM, egg82 wrote: break; was one of the first things I learned how to use while programming. When you have a loop searching through an array of 1 million elements, and you find the element you need at index 2, are you going to go through the rest of the 999, 998 elements? Seems pretty damn slow to me.

But you can also use boolean variables to indicate when you have located your element and tell your program to stop executing. Or you can use a return.

Its all preference on what you like to do.

Response to Error 1009 (tried everything) AS3 2012-07-28 02:16:15


Technically I could do this:

var stopLooping:Boolean = false;
for (var i:int = 0; i < 1000000; i++)
{
if (!stopLooping)
{
if (foundValue) stopLooping = true;
}
}
No indents, fukdapolice

If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to Error 1009 (tried everything) AS3 2012-07-28 02:27:07


At 7/28/12 02:16 AM, MintPaw wrote: Technically I could do this:

var stopLooping:Boolean = false;
for (var i:int = 0; i < 1000000; i++)
{
if (!stopLooping)
{
if (foundValue) stopLooping = true;
}
}

No indents, fukdapolice

that is what my professor called "using a flag". its bad practice and bad programming though. Its best to try to solve your solutions out without using "flags", "breaks","returns". Because by taking shortcuts your not truly learning what the real problem is.

Also i think your problem you explained could be solved with a simple if-else

Response to Error 1009 (tried everything) AS3 2012-07-28 02:29:15


At 7/28/12 02:15 AM, PMMurphy wrote: But you can also use boolean variables to indicate when you have located your element and tell your program to stop executing. Or you can use a return.

Its all preference on what you like to do.

yes, you could use a boolean. However:

var length: uint = array.length;
outerLoop: for(var i:uint = 0; i < length; i++){
	for(var i:uint = 0; i < length; i++){
		if(variable1 == array[i]){
			break outerLoop;
		}
	}
}

is much faster than (and uses less memory than)

var bool:Boolean = false
var length: uint = array.length;
for(var i:uint = 0; i < length; i++){
	if(bool){
		for(var i:uint = 0; i < length; i++){
			if(variable1 == array[i]){
				bool = true;
			}
		}
	}
}

Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to Error 1009 (tried everything) AS3 2012-07-28 02:32:24


Thats not the point to not using them when your learning. You need to understand whats going on and WHY it takes less memory and WHY its faster.

Not that its simply faster. You also need to know the bare basics to everything and the simple ways to solving problems. Because their may be a problem or something that comes along where you don't understand how to solve it with complex ways.

Yet again, its easy to argue this with your example because your using a proper way to use break.

When you are learning how to program, you should be focusing on your ability to problem solve. Efficiency and all that other stuff comes later.

Response to Error 1009 (tried everything) AS3 2012-07-28 02:34:02


At 7/28/12 02:29 AM, egg82 wrote: var bool:Boolean = false
var length: uint = array.length;
for(var i:uint = 0; i < length; i++){
if(bool){
for(var i:uint = 0; i < length; i++){
if(variable1 == array[i]){
bool = true;
}
}
}
}
var bool:Boolean = false;
var length:uint = array.length;
for(var i:uint = 0; i < length; i++){
	if(bool){
		for(var j:uint = 0; j < length; j++){
			if(variable1 == array[j]){
				bool = true;
			}
		}
	}
 }

fixed*
but still not recommended


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to Error 1009 (tried everything) AS3 2012-07-28 02:36:49


At 7/28/12 02:32 AM, PMMurphy wrote: Yet again, its easy to argue this with your example because your using a proper way to use break.

You keep saying "the proper way"

break; does precisely and exactly one thing, and it does it extremely well.
What's the "improper" way to use a break statement?


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to Error 1009 (tried everything) AS3 2012-07-28 02:36:55


Yeah yeah we all get it, if you know what you're doing break's obviously the way to go, but don't throw it everywhere or you'll you f stuff up. It's up to preference, there's no proper way to do anything really. Break has a plus as far as efficiency and readability. But I get it, some people throw _root. everywhere without knowing what it really does, although there are ways it can be useful.


If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to Error 1009 (tried everything) AS3 2012-07-28 03:01:06


At 7/27/12 08:47 PM, Barzona wrote: I've been working on this game (click to see) for awhile and, although it's doing what I want it to do so far, I'm still getting errors. The only specific error I'm getting is:

There are indeed some trouble makers in your code.
Let's first point those out that the error is not reporting.

You add 2 event listeners to each enemy.

public function Enemy()
{
addEventListener(Event.ENTER_FRAME, updateEnemy);
this.addEventListener(Event.ENTER_FRAME, removeEnemy);
}

There shouldn't be any, because adding them to every object will drop performance.
Instead iterate over the arrays that you store them in and call an "update" function.

You are doing busy waiting to check the health:

if(enemyHealth <= 0){

...and that's really bad.

You should do this in a setter function. Because this check should be performed when "enemyHealth" is reduced to 0, not when this ENTER_FRAME fires, which possibly happens to a different time.
Aside from the timing issue which is very unlikely to happen you are wasting a LOT of processing power:

Every enemy will die only once.
But you are calling that function on ENTER_FRAME, say 30 times a second.

Even if your enemy object is only living for 1 second (not that much, right?) you make 29 useless function calls.

---

You are adding/removing things to/from .stage which you shouldn't be doing.

stage.removeChild(this);

As a rule of thumb: object's don't add or remove themselves.

---

finally, the loops:

for (var i:int = 0; i < bulletArray.length; i++){
for(var j:int = 0; j < enemyArray.length; j++){
bullet = bulletArray[i];
enemy = enemyArray[j];
if (enemy.hitTestPoint(bullet.x, bullet.y, true)) {

soundChannel2 = zeldaHit.play();
stage.removeChild(bullet);
stage.addChild(impact);
impact.x = bullet.x;
impact.y = bullet.y;
enemy.enemyHealth --;
enemy.gotoAndPlay("enemyHit");
bulletArray.splice(i,1);

}
}
}

It's the same old story:
When you loop forward through an array while removing objects at the same time, you will skip elements.

To solve this, you could simply iterate backwards over your array.
Or, even simpler, use the for-each-in loop which will make sure every element gets touched.