I need to access a control for a geometry

I’m trying to do this, but a nullpointerexception is being thrown after a few clicks for some reason. I’ll show my code, and I’d appreciate it if anyone would tell me if I’m doing this correctly. If not, then please tell me how to correct this.

[java]if(results.getClosestCollision().getGeometry().
getControl(TowerControl.class) != null)[/java]

I guess you get a geometry as result but the control is on a parent spatial.

@normen said: I guess you get a geometry as result but the control is on a parent spatial.

I’m a newbie, and I don’t know what you mean by the parent spatial. Could you give me a quick explanation?

Sure:
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies

@normen said: Sure: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies

Ah, now I know what you’re referring to. In that case, that doesn’t seem to be what’s happening. I attached it directly to the geometry. Do you have any other ideas?
The most I did was

[java]tower.addControl(new TowerControl(this, 100, 0));
[/java]

tower is the actual geometry that had a control applied to it.

@coolman50544 said: I'm trying to do this, but a nullpointerexception is being thrown after a few clicks for some reason. I'll show my code, and I'd appreciate it if anyone would tell me if I'm doing this correctly. If not, then please tell me how to correct this.

[java]if(results.getClosestCollision().getGeometry().
getControl(TowerControl.class) != null)[/java]

For the record, if this is the line that threw an NPE then either results is null or it has no results. It cannot be otherwise.

Otherwise, you will have to show us the stack trace and the actual line. (I think I will have that tattooed somewhere as often as I say it. ;))

@pspeed said: For the record, if this is the line that threw an NPE then either results is null or it has no results. It cannot be otherwise.

Otherwise, you will have to show us the stack trace and the actual line. (I think I will have that tattooed somewhere as often as I say it. ;))

I’ll go ahead, and show more code.

This is pretty much where I’m getting results from. I’m selecting geometries from a visible cursor.
[java]ActionListener selectListener = new ActionListener() {
public void onAction(String name, boolean isPressed, float tpf) {
if(name.equals(MAPPING_SELECT_TOWER)) {
if(isPressed == false) {
CollisionResults results = new CollisionResults();
Vector2f click2d = inputManager.getCursorPosition();
Vector3f click3d = cam.getWorldCoordinates(new Vector2f(click2d.getX(),
click2d.getY()), 0f);
Vector3f dir = cam.getWorldCoordinates(new Vector2f(click2d.getX(),
click2d.getY()), 1f).
subtractLocal(click3d);
Ray ray = new Ray(click3d, dir);
rootNode.collideWith(ray, results);
if(results.getClosestCollision().getGeometry().
getControl(TowerControl.class) != null) {
TowerControl tower = results.getClosestCollision().getGeometry().
getControl(TowerControl.class);
selected.add(tower);
}
else
selected = null;
}
}
}
};[/java]

And here is the stack trace

java.lang.NullPointerException
at mygame.Main$1.onAction(Main.java:68)
at com.jme3.input.InputManager.invokeActions(InputManager.java:169)
at com.jme3.input.InputManager.onMouseButtonEventQueued(InputManager.java:433)
at com.jme3.input.InputManager.processQueue(InputManager.java:833)
at com.jme3.input.InputManager.update(InputManager.java:883)
at com.jme3.app.Application.update(Application.java:604)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:231)
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:744)[/java]

I’d also like to point out that I only get a nullpointerexception thrown when I don’t click on the geometry this code applies to. I tried testing results with results.size() != 0, but after rapidly clicking the exception is thrown once again.

rootNode.collideWith(ray, results);
if(results.getClosestCollision().getGeometry().
getControl(TowerControl.class) != null) {

…you don’t check to see if you have any results before just blindly grabbing at them. My guess is getClosestCollision() is null.

As a future tip: the debugger allows you to step through code and see these things in less time than it takes to write a forum post.

I used this to check results

[java] if(results.getClosestCollision() != null && results.getClosestCollision().getGeometry().
getControl(TowerControl.class) != null)[/java]

Thank you for your help so far, and this works almost perfectly. But if I click really rapidly, and then suddenly on the tower the same exception is thrown. What is going wrong ._.
And actually I just tried it again, and it seems I just have to click close to the tower; I’m pretty confused.

@coolman50544 said: I used this to check results

[java] if(results.getClosestCollision() != null && results.getClosestCollision().getGeometry().
getControl(TowerControl.class) != null)[/java] [/java]

Thank you for your help so far, and this works almost perfectly. But if I click really rapidly, and then suddenly on the tower the same exception is thrown. What is going wrong ._.
And actually I just tried it again, and it seems I just have to click close to the tower; I’m pretty confused.

It just can’t be the same NPE in the same place.

As a future tip: the debugger allows you to step through code and see these things in less time than it takes to write a forum post.

@pspeed said: It just can't be the same NPE in the same place.

As a future tip: the debugger allows you to step through code and see these things in less time than it takes to write a forum post.

Do you have any good tutorial videos to use the debugger effectively that you could direct me to?