Destroy body unsolvable null problem

Discuss issues specific to the Flash/AS3 port of Box2D
Posts: 21
Joined: Wed Feb 25, 2015 10:36 am

Destroy body unsolvable null problem

Postby gramcracker » Thu Jul 23, 2015 3:54 pm

I've been writing a flash game for 300 hours, and wrapping it up now.

I've solved all the errors except for one.
Unfortunately this is a fatal error I cannot solve, nor can anyone else.

When attempting to destroy bodies (which works fine all the time), the Box2D gives the following mystifying result.

Cannot access a property of a null reference - body.[b2Body].

Somehow, although the body clearly exists, it can't be destroyed since it is 'null'.
The body also has no texture, and no way to identify or distinguish it among others.
It has no width, no height. There is NO way to tell what this body is or why it brings the game to a halt when you destroy it.

I added the line, "if (body != null) and it still produces the null error above.

I tried adding textures to all bodies and it still produces the error.

I tried adding code inside b2World to make it 'null' after it's destroyed, but this doesn't work.

I tried iterating or getting information about the instance name, or the body or anything, but nothing.

The destruction happens by inserting items in an array, then deleting them all during the update() function.

I have not experienced this problem the first 200 hours of programming.

The glitch appears to happen when an exploding ball explodes, and the shrapnel hits the character.

I check for all types of collisions.

My only theory is that somehow after collision the object is being added to the DestroyList vector twice.

So Box2D is trying to destroy the object which has already been destroyed.

Even though I've placed safeguards so that objects can't be duplicated in the same array, and
all objects are spliced out when they are destroyed.

There is no point or way to post other code, it is 120 pages.

But this is the destroy code. The error always happens on Destroy.destroy().

private function destroyBodies():void

var k = Destructor.DestroyList.length;
while (k--)
var Destroying = Destructor.DestroyList[k];

if(Destroying!= null && Destroying.parent != null)


Destructor.DestroyList.length = 0;

Posts: 266
Joined: Sun Oct 25, 2009 3:28 am

Re: Destroy body unsolvable null problem

Postby jayther » Sun Jul 26, 2015 2:55 am

That error is indeed usually caused by trying to destroy a body that's already been destroyed. Maybe a body is being readded to the destroy list in another frame? Or maybe an item is sharing the same body?

Posts: 21
Joined: Wed Feb 25, 2015 10:36 am

Re: Destroy body unsolvable null problem

Postby gramcracker » Tue Jul 28, 2015 7:47 am

That was correct. I'll step through this, for the benefit of someone else, because it took me nearly two months to pin this down.

I destroyed bodies by adding them to a vector array in the custom contactlistener class.

In one particular case, I needed to add a destroyed body to the vector array OUTSIDE the contactlistener class, in another function (still within the loop).

Instead of doing this, I created another conditional in the contactlistener for this body type, and assigned a unique property to the body, IS_BOMB=true.

Then in the contactlistener class checked does Object_A == IS_BOMB or Object_B == IS_BOMB; If so, add Object A or B to the vector array.

The moral here seems to be that all insertion into body management vectors should be done from within your custom contactlistener class.

Return to “Flash”

Who is online

Users browsing this forum: No registered users and 3 guests