NullPointerException when calling Spatial.getWorldTranslation();

I have a mesh of similar objects, and I call Spatial.getWorldTranslation(); on the clicked cell (so I can display some text over the object). In about 5% cases, I get null pointer like this:

I’m not sure what causes this problem. The problem doesn’t seem to be persistent either; the following code never prints more than one warning.

[java]
boolean b = true;
while(b){
try{
Spatial.getWorldTranslation();
b = false;
} catch(NullPointerException ex){
System.out.println(“Warning: null pointer”);
}
}
[/java]

Which version of JME are you running?

3.0.10 stable.

Seems like somewhere a null light was added to your scene… either that or you are doing this from another thread or multithreading is at play somehow.

It’s most likely caused by multithreading, because I call it from outside the update thread. I’ve tried to use the synchronized context though, but it didn’t solve the problem.

@development said: It's most likely caused by multithreading, because I call it from outside the update thread. I've tried to use the synchronized context though, but it didn't solve the problem.

I have no idea what “synchronized context” means in this case because you’d have to synchronize the whole update thread… the sledge hammer approach to killing a moth.

Just don’t access spatials from more than one thread and you will be fine. We’d need to know more about what you are doing to offer better advice than that.

@pspeed said: I have no idea what "synchronized context" means in this case because you'd have to synchronize the whole update thread... the sledge hammer approach to killing a moth.

That’s pretty much what I was doing… Anyway, I’ve modified the code to get the world translation from within the update thread as you’ve suggested and it solved my problem. But now I have another problem: on Android, when I start the game activity from the main activity, I get the following error:

[java]com.jme3.renderer.RendererException: An OpenGL error has occurred: invalid value
at com.jme3.renderer.android.RendererUtil.checkGLError(RendererUtil.java:125)
at com.jme3.renderer.android.OGLESShaderRenderer.updateUniform(OGLESShaderRenderer.java:657)
at com.jme3.renderer.android.OGLESShaderRenderer.updateShaderUniforms(OGLESShaderRenderer.java:774)
at com.jme3.renderer.android.OGLESShaderRenderer.setShader(OGLESShaderRenderer.java:978)
at com.jme3.material.Material.renderMultipassLighting(Material.java:837)
at com.jme3.material.Material.render(Material.java:1107)
at com.jme3.renderer.RenderManager.renderGeometry(RenderManager.java:523)
at com.jme3.renderer.queue.RenderQueue.renderGeometryList(RenderQueue.java:322)
at com.jme3.renderer.queue.RenderQueue.renderQueue(RenderQueue.java:374)
at com.jme3.renderer.RenderManager.renderViewPortQueues(RenderManager.java:763)
at com.jme3.renderer.RenderManager.flushQueue(RenderManager.java:719)
at com.jme3.renderer.RenderManager.renderViewPort(RenderManager.java:983)
at com.jme3.renderer.RenderManager.render(RenderManager.java:1029)
at game.Game.update(Game.java:232)
at com.jme3.system.android.OGLESContext.onDrawFrame(OGLESContext.java:349)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1525)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1242)[/java]

This problem only appears when I start the game activity from another activity. When I modify the Android Manifest to start the game activity directly on app start, it works as it has to.

@development said: But now I have another problem: on Android, when I start the game activity from the main activity, I get the following error:

[java]com.jme3.renderer.RendererException: An OpenGL error has occurred: invalid value
at com.jme3.renderer.android.RendererUtil.checkGLError(RendererUtil.java:125)
at com.jme3.renderer.android.OGLESShaderRenderer.updateUniform(OGLESShaderRenderer.java:657)
at com.jme3.renderer.android.OGLESShaderRenderer.updateShaderUniforms(OGLESShaderRenderer.java:774)
at com.jme3.renderer.android.OGLESShaderRenderer.setShader(OGLESShaderRenderer.java:978)
at com.jme3.material.Material.renderMultipassLighting(Material.java:837)
at com.jme3.material.Material.render(Material.java:1107)
at com.jme3.renderer.RenderManager.renderGeometry(RenderManager.java:523)
at com.jme3.renderer.queue.RenderQueue.renderGeometryList(RenderQueue.java:322)
at com.jme3.renderer.queue.RenderQueue.renderQueue(RenderQueue.java:374)
at com.jme3.renderer.RenderManager.renderViewPortQueues(RenderManager.java:763)
at com.jme3.renderer.RenderManager.flushQueue(RenderManager.java:719)
at com.jme3.renderer.RenderManager.renderViewPort(RenderManager.java:983)
at com.jme3.renderer.RenderManager.render(RenderManager.java:1029)
at game.Game.update(Game.java:232)
at com.jme3.system.android.OGLESContext.onDrawFrame(OGLESContext.java:349)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1525)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1242)[/java]

This problem only appears when I start the game activity from another activity. When I modify the Android Manifest to start the game activity directly on app start, it works as it has to.

Managed to solve this error, the problem was that I didn’t reload all materials when switching from one activity to another.