Null pointer exception in collision listener on Android

Uh, ok. So you updated to nightly when you last told me to update and it still would not work?

Yes, when you told me to get the latest update I thought you meant the nightly version but if this is not correct I may uninstall the SDK completely and go back to the stable release.

Anyway, updating to the nightly version did not solve the problem since I still get the null pointer (same behavior as with the stable version)

Tomorrow I will install jmonkey Stable version from scratch on a Windows XP machine and see if the same problem occurs (my current machine is Windows 7).

I will let you know. I want to thank you Normen for all the support and time you have dedicated to this problem today. I really appreciate it.

1 Like

I don’t know if this is related to your problem or not, but on Android, I ran into npe on the collision event as well. I’m using the code below. I found that if I didn’t filter the event to “case PhysicsCollisionEvent.TYPE_PROCESSED”, then one or both of the objects were null. I don’t know if this happens on desktops or not, but I suspected that since Android ran slower, the situation occured.





[java]

public void collision(PhysicsCollisionEvent pce) {

if (pce == null) {

// logger.log(Level.INFO, “Collision: pce is NULL”);

return;

} else {

// logger.log(Level.INFO, “Collision: pce is not NULL”);

}



switch (pce.getType()) {

case PhysicsCollisionEvent.TYPE_ADDED:

// logger.log(Level.INFO, “Collision: Collision Event Added”);

return;

case PhysicsCollisionEvent.TYPE_DESTROYED:

// logger.log(Level.INFO, “Collision: Collision Event Destroyed”);

return;

case PhysicsCollisionEvent.TYPE_PROCESSED:

Spatial tmpObjA = pce.getNodeA();

Spatial tmpObjB = pce.getNodeB();

// logger.log(Level.INFO, “Collision: Collision Event Processed”);

if (tmpObjA == null) {

logger.log(Level.INFO, “Collision: NodeA is NULL”);

return;

}

if (tmpObjB == null) {

logger.log(Level.INFO, “Collision: NodeB is NULL”);

return;

}

Ball tmpBall = curBall.getBall();

Spatial tmpBag = tmpBall.getGeometry();

if (tmpObjA.equals(tmpBag) || tmpObjB.equals(tmpBag)) {

logger.log(Level.INFO, “Collision: NodeA ({0}) is a Bag”, tmpObjA.getName());

tmpBall.explode();

}

break;

default:

logger.log(Level.INFO, “Collision: Unknown Collision Type: {0}”,pce.getType());

return;

}

}

}



[/java]

2 Likes

Brilliant! Yes!, this solved the problem!

Thanks a lot iwgeric!

Normen, maybe this could be mentioned in the tutorials or maybe only TYPE_PROCESSED collision events should be notified to the listener to avoid this problem.

Thanks both for your time!

Hm, it should only call them with processed messages… I’ll check the collision callback code hat was recently added.

Hi Normen,

see attached a trace of the execution of my program. Each time you see “Non processed event” is because I receive in my callback function an event whose TYPE is different from PhysicsCollisionEvent.TYPE_PROCESSED.

As you can see the type is always 0 for non processed events.

I have observed that these non processed events are triggered at the beginning of the execution of my program (first 4 or 5 collisions) and then they are gone!.

Hope this helps



*************************

INFO: Adding RigidBody 73.931.776 to physics space.

Non processed Event, TYPE is = 0

ENTRY Speed: 20.0 0.0 0.0

Hit at: -4.1723249E-7 -6.8500004 -0.10000014

Non processed Event, TYPE is = 0

Non processed Event, TYPE is = 0

Non processed Event, TYPE is = 0

28-dic-2011 20:56:28 com.jme3.scene.Node attachChild

INFO: Child (null) attached to this node (DebugShapeNode)

PastryManager <0x43A5B0…> sending to <0x43A5B0…>

EXIT Speed: 20.0 0.0 0.0

Hit at: -4.172325E-7 -6.8500004 -0.10000205

28-dic-2011 20:56:31 com.jme3.scene.Node attachChild

INFO: Child (object) attached to this node (Root Node)

28-dic-2011 20:56:31 com.jme3.bullet.objects.infos.RigidBodyMotionState

INFO: Created MotionState 5e3f68

28-dic-2011 20:56:31 com.jme3.bullet.collision.shapes.BoxCollisionShape createShape

INFO: Created Shape 5e4010

28-dic-2011 20:56:31 com.jme3.bullet.objects.PhysicsRigidBody rebuildRigidBody

INFO: Created RigidBody 46838e0

28-dic-2011 20:56:31 com.jme3.bullet.collision.PhysicsCollisionObject initUserPointer

INFO: initUserPointer() objectId = 46838e0

28-dic-2011 20:56:31 com.jme3.bullet.PhysicsSpace addRigidBody

INFO: Adding RigidBody 73.939.168 to physics space.

ENTRY Speed: 20.0 0.0 0.0

Hit at: 3.0 -5.8500004 -0.1

Non processed Event, TYPE is = 0

Non processed Event, TYPE is = 0

Non processed Event, TYPE is = 0

28-dic-2011 20:56:32 com.jme3.scene.Node attachChild

INFO: Child (null) attached to this node (DebugShapeNode)

PastryManager <0x43A5B0…> sending to <0x43A5B0…>

EXIT Speed: 20.0 0.0 0.0

Hit at: 3.0 -5.8500004 -0.1000019

28-dic-2011 20:56:35 com.jme3.scene.Node attachChild

INFO: Child (object) attached to this node (Root Node)

28-dic-2011 20:56:35 com.jme3.bullet.objects.infos.RigidBodyMotionState

INFO: Created MotionState 4681e78

1 Like