onAnalog Input NPE

I’m having a strange issue when using mouse picking in the onAnalog call. I get a NPE when it starts looking at the results if I mouse over the windows task bar with the left mouse button down or if I adjust the app resolution slightly so there are black bars on the sides and mouse over those with the left button down. I can catch the exception and continue on just fine, it doesn’t seem to have any further effect but I don’t under stand why its happening. Any help would be great.

[java]
Apr 9, 2013 2:05:32 PM de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processMouseEvent
INFO: [processMouseEvent] [467, 864, 0, 0, false] processed [false]
java.lang.NullPointerException
at fruitSlicer.FruitSlicer$1.onAnalog(FruitSlicer.java:203)
at com.jme3.input.InputManager.invokeAnalogs(InputManager.java:245)
at com.jme3.input.InputManager.invokeTimedActions(InputManager.java:202)
at com.jme3.input.InputManager.onMouseButtonEventQueued(InputManager.java:434)
at com.jme3.input.InputManager.processQueue(InputManager.java:835)
at com.jme3.input.InputManager.update(InputManager.java:885)
at com.jme3.app.Application.update(Application.java:606)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:230)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
at java.lang.Thread.run(Thread.java:662)
[/java]

onAnalog
[java]
private AnalogListener analogListener = new AnalogListener() {
public void onAnalog(String name, float value, float tpf) {
if (name.equals(“drag”)) {
CollisionResults results = new CollisionResults();
Vector2f mouseCoords = inputManager.getCursorPosition();
Vector3f location = cam.getWorldCoordinates(mouseCoords, 30).clone();
Vector3f direction = cam.getWorldCoordinates(mouseCoords, -10).clone();
direction.subtractLocal(location).normalizeLocal();
Ray ray = new Ray(location, direction);
fruitNode.collideWith(ray, results);
try {
if (!“Wall”.equals(results.getClosestCollision().getGeometry().getName())){ //<-----line 203
shockwave.killAllParticles();
explosionEffect.setLocalTranslation(results.getClosestCollision().getGeometry().getLocalTranslation());
shockwave.emitAllParticles();
results.getClosestCollision().getGeometry().removeFromParent();
gameScore++;
}
else {
CollisionResult closest = results.getClosestCollision();
trailNode.setLocalTranslation(closest.getContactPoint());
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
};
[/java]

Most probably you’re getting a null when checking for collision. Check the collision first then if you get something, check if it’s “Wall”.

Pseudo-code:

[java]
if (collision != null) {
if (collision.geometry.name.equals(“Wall”) {
// etc
}
}
[/java]

NPE could be the Collision or the geometry. I’m not sure exactly which one would be more likely to throw you a null back off the top of my head.

Awesome that was it. I had that in there in an earlier revision but it was taken out at some point. Thanks.

[java]
private AnalogListener analogListener = new AnalogListener() {
public void onAnalog(String name, float value, float tpf) {
if (name.equals(“drag”)) {
CollisionResults results = new CollisionResults();
Vector2f mouseCoords = inputManager.getCursorPosition();
Vector3f location = cam.getWorldCoordinates(mouseCoords, 30).clone();
Vector3f direction = cam.getWorldCoordinates(mouseCoords, -10).clone();
direction.subtractLocal(location).normalizeLocal();
Ray ray = new Ray(location, direction);
fruitNode.collideWith(ray, results);
if (results.size() > 0) {
if (!“Wall”.equals(results.getClosestCollision().getGeometry().getName())){
shockwave.killAllParticles();
explosionEffect.setLocalTranslation(results.getClosestCollision().getGeometry().getLocalTranslation());
shockwave.emitAllParticles();
results.getClosestCollision().getGeometry().removeFromParent();
gameScore++;
}
else {
CollisionResult closest = results.getClosestCollision();
trailNode.setLocalTranslation(closest.getContactPoint());
}
}
}
}
};
[/java]