[SOLVED] NullPointerException: the native object does not exist

that’s the error name and it comes on this code:

   ballphy = new RigidBodyControl(5f); //(ballphy is created)
   ballphy.setCcdSweptSphereRadius(1f);  //(here cones the error)
   ballgeo.addControl(ballphy);
   state.getPhysicsSpace().add(allphy);  //(state is of BulletAppState)
1 Like

You haven’t set a collisionshape.

The method setCcdSweptSphereRadius bubbles up to a RigidBody object called rBody which is only created when you set a collision shape.

Take a look at the static method CollisionShapeFactory.createDynamicMeshShape(Spatial spatial).

https://javadoc.jmonkeyengine.org/com/jme3/bullet/util/CollisionShapeFactory.html

Thanks i will try it

Is the trouble solved?

image

Fix

@ValeriiNikitin

i belive your ballMesh is not Sphere object mesh. As you can see below you can also use: SphereCollisionShape

Because if it is sphere class object then

ballphy = new RigidBodyControl(5f);

should auto choose proper CollisionShapeFactory.

Here is how it choose based on what Spatial is:

(jmonkeyengine/RigidBodyControl.java at master · jMonkeyEngine/jmonkeyengine · GitHub)

/**
 * Set the collision shape based on the controlled spatial and its
 * descendents.
 */
protected void createCollisionShape() {
    if (spatial == null) {
        return;
    }
    if (spatial instanceof Geometry) {
        Geometry geom = (Geometry) spatial;
        Mesh mesh = geom.getMesh();
        if (mesh instanceof Sphere) {
            collisionShape = new SphereCollisionShape(((Sphere) mesh).getRadius());
            return;
        } else if (mesh instanceof Box) {
            collisionShape = new BoxCollisionShape(new Vector3f(((Box) mesh).getXExtent(), ((Box) mesh).getYExtent(), ((Box) mesh).getZExtent()));
            return;
        }
    }
    if (mass > 0) {
        collisionShape = CollisionShapeFactory.createDynamicMeshShape(spatial);
    } else {
        collisionShape = CollisionShapeFactory.createMeshShape(spatial);
    }
}

its correct what you do, i just want mention that your situation is probably different because Mesh is not Sphere object so it cant determine how to generate it.

also it would be better if you provide this in constructor so this do not calculate it earlier itself.

ballMesh is Sphere.

I hope that I understood properly:

%D0%B8%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5

But this way I like more:

%D0%B8%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5

%D0%B8%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5

it will work too. :slight_smile: myself i use this way. But looks like you are missing “mass” param.
Dynamic objects need mass.

RigidBodyControl bullet = new RigidBodyControl(new SphereCollisionShape(0.1f), 0.0001f);

but if your ballMesh is Sphere, then this should just work without manually create collisionmesh.

so

RigidBodyControl bullet = new RigidBodyControl(0.0001f);

should just work for Sphere mesh, see wiki below example:

https://wiki.jmonkeyengine.org/jme3/beginner/hello_physics.html

this should just work like is here. if not then wiki need updates or there is some bug.

In this code occurs exception:
%D0%B8%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5

Stacktrace:
{code}
вер. 06, 2019 1:11:55 PM com.jme3.app.LegacyApplication handleError
SEVERE: Uncaught exception thrown in Thread[jME3 Main,5,main]
java.lang.NullPointerException: The native object does not exist.
at com.jme3.bullet.objects.PhysicsRigidBody.setCcdSweptSphereRadius(Native Method)
at com.jme3.bullet.objects.PhysicsRigidBody.setCcdSweptSphereRadius(PhysicsRigidBody.java:380)
at mygame.Main.shootCannonBall(Main.java:149)
at mygame.Main$1.onAction(Main.java:160)
at com.jme3.input.InputManager.invokeActions(InputManager.java:169)
at com.jme3.input.InputManager.onMouseButtonEventQueued(InputManager.java:446)
at com.jme3.input.InputManager.processQueue(InputManager.java:864)
at com.jme3.input.InputManager.update(InputManager.java:914)
at com.jme3.app.LegacyApplication.update(LegacyApplication.java:724)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:227)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:197)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:232)
at java.lang.Thread.run(Thread.java:748)
{code}

You need to add a collision shape.

move

ballGeo.addControl(ballPhy)

just after new RigidBody, so CCD methods will be after, i think this will fix.

@jayfella collision shape should auto-add on spatial assign. (control assign)

Thanks guys

this works now, when you moved it, right?

Yes, now works fine
%D0%B8%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5

2 Likes