Problem with click a model

in my game, when a model is clicked, an event must occur



but this only works when you click a BOX, SPHERE … when you click a model.j3o, an error occurs.



my function:



private void initClick()

{

inputManager.addMapping("Shoot",

new KeyTrigger(KeyInput.KEY_F1), // trigger 1: spacebar

new MouseButtonTrigger(1)); // trigger 2: left-button click

inputManager.addListener(actionListener, "Shoot");

}



private ActionListener actionListener = new ActionListener()

{

@Override

public void onAction(String name, boolean keyPressed, float tpf)

{

if (name.equals("Shoot") && !keyPressed)

{

// 1. Reset results list.

CollisionResults results = new CollisionResults();

// 2. Aim the ray from cam loc to cam direction.

Ray ray = new Ray(cam.getLocation(), cam.getDirection());

// 3. Collect intersections between Ray and Shootables in results list.

//andares[indexAndar].pegaApIndex().pegaComodoIndex().getNode().collideWith(ray, results);

rootNode.collideWith(ray, results);

// 4. Print the results.

System.out.println("


Collisions? " + results.size() + "
");
for (int i = 0; i < results.size(); i++) {
// For each hit, we know distance, impact point, name of geometry.
float dist = results.getCollision(i).getDistance();
Vector3f pt = results.getCollision(i).getContactPoint();
String hit = results.getCollision(i).getGeometry().getName();
if (hit != null)
{
System.out.println("* Collision #" + i);
System.out.println(" You shot " + hit + " at " + pt + ", " + dist + " wu away.");
click (hit);
}
}

}
}
};


error when click a model.j3o:


Collisions? 2
Exception in thread "LWJGL Renderer Thread" java.lang.NullPointerException
at V3D.Controller.Cena$1.onAction(Cena.java:195)
at com.jme3.input.InputManager.invokeActions(InputManager.java:112)
at com.jme3.input.InputManager.onMouseButtonEvent(InputManager.java:271)
at com.jme3.input.lwjgl.LwjglMouseInput.update(LwjglMouseInput.java:74)
at com.jme3.input.InputManager.update(InputManager.java:394)
at com.jme3.app.Application.update(Application.java:392)
at com.jme3.app.SimpleBulletApplication.update(SimpleBulletApplication.java:270)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:112)
at com.jme3.system.lwjgl.LwjglCanvas.runLoop(LwjglCanvas.java:190)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:162)
at java.lang.Thread.run(Thread.java:619)



line 195: String hit = results.getCollision(i).getGeometry().getName();

Can someone help me?

up!



anyone?

The NPE is is your own code, this is not an engine issue.

Hello!



I’m with Normen. I saw this post when you originally came up and thought I’d be able to figure it out, but there’s really just not enough information to troubleshoot it accurately. Post more code I guess :shrug:



If I had to take a shot in the dark, when I originally looked at this I thought maybe the geometry you’ve got there just has no name, so it NPE’s. But it’s just a string, so I’d be wondering why it wouldn’t work as is and you’d just get a null string. Also, given where it NPE’s, is there a possibility that the collision you’re checking does not have a geometry? If you do a hit collision test on just a bounding box, it’s going to go boom when it tries to get the geometry.



That’s all I got. :<



~FlaH