Half-random NullPointerException in OpenAL

This is pretty suspicious stuff. Most of the time, the program runs fine. But sometimes, we get the following stacktrace (given in context - not that it matters).

22-jun-2007 14:08:30 com.jme.util.lwjgl.LWJGLTimer <init>
INFO: Timer resolution: 1000 ticks per second
22-jun-2007 14:08:30 com.jmex.game.DefaultUncaughtExceptionHandler uncaughtException
SEVERE: Main game loop broken by uncaught exception
java.lang.NullPointerException
        at com.jmex.sound.openAL.SoundSystem.update(Unknown Source)
        at com.jmex.game.StandardGame.update(Unknown Source)
        at com.jmex.game.StandardGame.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
22-jun-2007 14:08:30 com.jmex.sound.openAL.scene.SoundNode <init>
INFO: Node created.



On another run, the position might be slightly different (below the SoundNode constructor, for example). So, this sounds a damn lot like a racing condition of some sorts. I started digging at the sources and the only suspicious line I could find is line 202 in com.jmex.sound.openAL.SoundSystem.java

nodes[nodeName].updateWorldData(time);



Proposed patch:

Index: src/com/jmex/sound/openAL/SoundSystem.java
===================================================================
RCS file: /cvs/jme/src/com/jmex/sound/openAL/SoundSystem.java,v
retrieving revision 1.15
diff -u -r1.15 SoundSystem.java
--- src/com/jmex/sound/openAL/SoundSystem.java   6 Mar 2007 15:30:00 -0000   1.15
+++ src/com/jmex/sound/openAL/SoundSystem.java   22 Jun 2007 12:26:00 -0000
@@ -199,7 +199,8 @@
     public static void update(int nodeName, float time){
         if(nodes==null) return;
         if(nodeName<0 || nodeName>=nodes.length) return;
-        nodes[nodeName].updateWorldData(time);
+        if(nodes[nodeName] != null)
+            nodes[nodeName].updateWorldData(time);
         updateListener();
        
     }



Because of the random nature of this crash, I can't tell for sure this solves it. I haven't had any problems in the couple of runs I did with patched sources, I'll tell you if I have. And perhaps the root of the problem is deeper than this. Have fun figuring it out.

If you compiled jME with debug option on wouldn't it tell you the exact line saving the guess work?

Err… Probably yes. But then, I still have to get that crash again…

Well, I got the NullPointerException again. Now I'm officially confused. Researching this tonight.



Hmm. Of course, it was the other update getting called. New patch looks something like

Index: src/com/jmex/sound/openAL/SoundSystem.java
===================================================================
RCS file: /cvs/jme/src/com/jmex/sound/openAL/SoundSystem.java,v
retrieving revision 1.15
diff -u -r1.15 SoundSystem.java
--- src/com/jmex/sound/openAL/SoundSystem.java   6 Mar 2007 15:30:00 -0000   1.15
+++ src/com/jmex/sound/openAL/SoundSystem.java   26 Jun 2007 21:10:42 -0000
@@ -185,7 +185,8 @@
         if(nodes==null) return;
        
         for(int a=0; a<nodes.length; a++){
-            nodes[a].updateWorldData(time);
+           if(nodes[a] != null)
+              nodes[a].updateWorldData(time);
         }
         updateListener();
        
@@ -199,7 +200,8 @@
     public static void update(int nodeName, float time){
         if(nodes==null) return;
         if(nodeName<0 || nodeName>=nodes.length) return;
-        nodes[nodeName].updateWorldData(time);
+        if(nodes[nodeName] != null)
+            nodes[nodeName].updateWorldData(time);
         updateListener();
        
     }



I've noticed a lot of those 'unchecked' array references that might be null if threads decide to intermingle. This is of course not unlikely (hell, it happened to me and I'm Mr. Center-of-the-Gauss-Curve) if StandardGame's update() gets called every few moments while complex objects are being moved around in the createSoundNode code.

Good points…  I will check the jmex.audio package for similar problems as jmex.sound will eventually be dropped.

i just wanted to add, that i get the same Exeption maybe once ever 10 starts.