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.
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
@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.
@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.
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.
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.