NullPointerException in rootNode.findPick

I thought I should experiment with selecting objects and see if I could learn anything. Unfortunately I get a NullPointerException outside of my code when using rootNode.findPick.



I’ve altered the example HelloMousePick’s simpleUpdate to check for a pick the same way as the jmetest.intersection.MousePick class does it. I.e instead of having the box check the intersection with the ray I’m using the rootNode.findPick( mouseRay, pr ).



And here we got the stack trace:


java.lang.NullPointerException
        at com.jme.scene.Geometry.findPick(Geometry.java:1032)
        at com.jme.scene.Node.findPick(Node.java:363)
        at se.pmd.test.jme.HelloMousePick.simpleUpdate(HelloMousePick.java:100)
        at com.jme.app.SimpleGame.update(SimpleGame.java:120)
        at com.jme.app.BaseGame.start(BaseGame.java:68)
        at se.pmd.test.jme.HelloMousePick.main(HelloMousePick.java:41)



It would seem that in the second iteration of the for-loop in Node.findPick an object cast to Spatial got a worldBound that is null and when this is used later on it results in the exception. That's as far as I got ;)

Well, I didn’t put any null checks (I was thinking of speed). I assumed that each Geometry checked against would have ModelBounds (and thus WorldBounds) set, but that might not be the case. I can put the null checks in, but I’d be interested in knowing why you have a Geometry without a Model bounds.

Well, there should be a BoundingBox assigned to the Box object and updated with a box.updateModelBound(). I’ve not changed any of that from the example so it should have bounds. If I change that to a sphere instead I can see it in the scene when pressing B so I’m sure it’s there.



If I don’t add any BoundingBox/Sphere at all I get this instead:

java.lang.NullPointerException
        at com.jme.scene.Node.findPick(Node.java:360)
        at se.pmd.test.jme.HelloMousePick.simpleUpdate(HelloMousePick.java:100)
        at com.jme.app.SimpleGame.update(SimpleGame.java:120)
        at com.jme.app.BaseGame.start(BaseGame.java:68)
        at se.pmd.test.jme.HelloMousePick.main(HelloMousePick.java:41)



So without BoundingBox it won't come to the Geometry class, which it does with a BoundingBox.

I’m guessing that fonts are attached to the rootnode, and they are being check, and they don’t have a bounds either. So, I suppose having a null check is appropriate in this case and it would be very easy to assume picking the root node will give you the results you desire. I’ll add the checks.

BTW, can you post your case so I can work off your example?

You already got most of the source in jmetest.TutorialGuide.HelloMousePick. All I changed was a few lines in simpleUpdate, it looks like this now:


      protected void simpleUpdate(){
            // Get the mouse input device from the jME mouse
            MouseInput thisMouse=am.getMouseInput();
            // Is button 0 down?   Button 0 is left click
            if (thisMouse.isButtonDown(0)){
                  Vector2f screenPos=new Vector2f();
                  // Get the position that the mouse is pointing to
                  screenPos.set(thisMouse.getXAbsolute(),thisMouse.getYAbsolute());
                  //screenPos.set(am.getHotSpotPosition().x,am.getHotSpotPosition().y);
                  // Get the world location of that X,Y value
                  Vector3f worldCoords=display.getWorldCoordinates(screenPos,0);
                  // Create a ray starting from the camera, and going in the direction of the mouse's location
                  Ray mouseRay=new Ray(cam.getLocation(),worldCoords.subtractLocal(cam.getLocation()));
                  // Does the mouse's ray intersect the box's world bounds?
                  PickResults pr = new BoundingPickResults();
                  rootNode.findPick( mouseRay, pr );
                  //if( b.getWorldBound().intersects( mouseRay ) ) {
                  //   b.setRandomColors();
                  //}
            }
      }



And if you wonder why I didn't do anything with the pick results it's because I found the bug before I added that :)

I believe in that I attached my AbsoluteMouse to the root, when I should of the fpsnode. That could be the ‘root’ of your problem. Change that and try again?

That’s it! Now it’s working. Thanks :slight_smile:



So, let’s see if I got this right. The fpsNode isn’t a child of the rootNode so adding things without a bounding box like the mouse object to that node will exclude it from the rootNode.findPick.



edit: correct?

I went ahead and added the null check, as I think it may be fairly common event, and updated the HelloMousePick to use the PickResults for the tutorial. Committed now.

Sounds good.



Thanks for the very quick response both of you :slight_smile: