NullPointerExceptions :-(

I am using jME 2. I went to this tutorial: http://www.jmonkeyengine.com/wiki/doku.php?id=simplegame_to_standardgame and copied the code straight from it to see if it would work. I got these errors when running it:

init:

deps-jar:

compile:

run:

Sep 13, 2008 6:55:45 PM com.jme.input.joystick.DummyJoystickInput <init>

INFO: Joystick support is disabled

Sep 13, 2008 6:55:45 PM com.jme.system.lwjgl.LWJGLDisplaySystem <init>

INFO: LWJGL Display System created.

Sep 13, 2008 6:55:45 PM com.jme.renderer.lwjgl.LWJGLRenderer <init>

INFO: LWJGLRenderer created. W:  640H: 480

Sep 13, 2008 6:55:45 PM com.jme.renderer.AbstractCamera <init>

INFO: Camera created.

Sep 13, 2008 6:55:46 PM com.jmex.audio.openal.OpenALSystem setupSourcePool

INFO: max source channels: 64

Sep 13, 2008 6:55:46 PM com.jmex.game.state.GameStateManager create

INFO: Created GameStateManager

Sep 13, 2008 6:55:46 PM com.jme.util.lwjgl.LWJGLTimer <init>

INFO: Timer resolution: 1000 ticks per second

Sep 13, 2008 6:55:46 PM com.jme.scene.Node <init>

INFO: Node created.

Sep 13, 2008 6:55:46 PM com.jme.scene.Node <init>

INFO: Node created.

Sep 13, 2008 6:55:46 PM com.jme.scene.Node attachChild

INFO: Child (Text) attached to this node (TextNode)

Sep 13, 2008 6:55:46 PM com.jme.scene.Node <init>

INFO: Node created.

Sep 13, 2008 6:55:46 PM com.jme.scene.Node <init>

INFO: Node created.

Sep 13, 2008 6:55:46 PM com.jmex.game.state.StatisticsGameState setupGraph

SEVERE: Statistics cannot be displayed if Debug.stats is false, enable gathering of statistics first: System.setProperty("jme.stats", "set");

Sep 13, 2008 6:55:46 PM com.jme.scene.Node <init>

INFO: Node created.

Exception in thread "main" java.lang.NullPointerException

        at com.jme.renderer.lwjgl.LWJGLTextureRenderer.<init>(LWJGLTextureRenderer.java:103)

        at com.jme.system.lwjgl.LWJGLDisplaySystem.createTextureRenderer(LWJGLDisplaySystem.java:321)

        at com.jme.util.stat.graph.AbstractStatGrapher.<init>(AbstractStatGrapher.java:68)

        at com.jme.util.stat.graph.LineGrapher.<init>(LineGrapher.java:90)

        at com.jme.util.stat.graph.GraphFactory.makeLineGraph(GraphFactory.java:69)

        at com.jmex.game.state.StatisticsGameState.setupStatGraphs(StatisticsGameState.java:149)

        at com.jmex.game.state.StatisticsGameState.setupGraph(StatisticsGameState.java:113)

        at com.jmex.game.state.StatisticsGameState.<init>(StatisticsGameState.java:98)

        at com.jmex.game.state.DebugGameState.init(DebugGameState.java:88)

        at com.jmex.game.state.DebugGameState.<init>(DebugGameState.java:81)

        at com.jmex.game.state.DebugGameState.<init>(DebugGameState.java:76)

        at demoapp.Main.main(Main.java:28)

Java Result: 1

BUILD SUCCESSFUL (total time: 7 seconds)

Oh no! NullPointerExceptions! Please tell me I am not gonna die. I got the config screen at the beginning.


SEVERE: Statistics cannot be displayed if Debug.stats is false, enable gathering of statistics first: System.setProperty("jme.stats", "set");



A severe loglevel message normaly means there is something wrong - and this msg says you have to set "jme.stats" to set bevore calling game.start().

Then the statistics gamestate would work and not throwing a npe?

Greetz

The problem is that the DebugGamestate needs to be created in the opengl thread. (i guess this should be fixed :confused: )

Use a GameTaskQueue to create the DebugGamestate.



The message about Debug.stats is not very important, it just means that no statistics an get displayed, but it won't throw a NPE because of this.

Well, I got it to run by using the OpenGL thread and am trying to get the stats to work. Thanks.

I couldn't get the stats working. What line of code should I put in, or how do I disable it?

SEVERE: Statistics cannot be displayed if Debug.stats is false, enable gathering of statistics first: System.setProperty("jme.stats", "set");


System.setProperty("jme.stats", "set");

Thanks.

I get this exception when starting ModelLoader as well. Did StandardGame change in some way? Should the tests or the GameStates be corrected?

mhh yeah i'm afraid i introduced that problem when we inserted StatisticsGamestate into DebugGamestate.

The StatisticsGamestate needs to be created in the OpenGL Thread because in the LineGrapher constructor a TextureRenderer is created.

And the TextureRenderer still needs to be created in the OpenGL thread.



Possible solutions:

  • remove StatisticsGamestate from DebugGamestate again.
  • make TextureRenderer lazy initialize the OpenGL stuff.
  • Initialize/create states from within the OpenGL thread.

What about changing DebugGamestate to support creation in non-GL threads? (queue the texture initialization etc.)

hmm i'll try that.

yesterday i tried changing TextureRenderer, but thats a bit complicated  :slight_smile:



edit:

hmm ok that was easy:

the following patch creates and adds the StatisticsGamestate in the GameStates update method and such in the OpenGL thread.

And that way, the DebugGameState can be created outside the OpenGL thread.



Is someone Subclasses from DebugGameState and overwrites the update Method the StatisticsGamestate will never be created tho …

Maybe StatisticsGameState  can be changed so that the LineGraph stuff will be initialized in its update method.



Index: src/com/jmex/game/state/DebugGameState.java

===================================================================

— src/com/jmex/game/state/DebugGameState.java (revision 4029)

+++ src/com/jmex/game/state/DebugGameState.java (working copy)

@@ -71,7 +71,8 @@

    protected boolean showBounds = false;

    protected boolean showDepth = false;

    protected boolean showNormals = false;

-

+    protected boolean initialized = false;

+   

    public DebugGameState() {

    this(true);

    }

@@ -85,7 +86,6 @@

        rootNode = new Node("RootNode");



        // create a statistics game state

-        GameStateManager.getInstance().attachChild(new StatisticsGameState("stats", 1f, 0.25f, 0.75f, true));

       

        // Create a wirestate to toggle on and off. Starts disabled with default

        // width of 1 pixel.

@@ -168,6 +168,11 @@



    public void update(float tpf) {

    super.update(tpf);

+        if (initialized == false) {

+        GameStateManager.getInstance().attachChild(new StatisticsGameState("stats", 1f, 0.25f, 0.75f, true));

+        initialized = true;

+        }

+

        // Update the InputHandler

    if (input != null) {

    input.update(tpf);

hmm even better, a similar fix can be done in the StatisticsGamestate.

That way, no change is needed in the DebugGamestate.



proposed fix to StatisticsGamestate

http://www.jmonkeyengine.com/jmeforum/index.php?topic=9331.0



I don't really agree with this.

If a State needs to be "instantiated" within the OpenGL thread, that should be documented and that's enough. In my opinion you are fixing something that should be up to users to take care of.

StatisticsGameState was perfect as it was.

Many generic GameStates need to load resources and it's not their problem if they are initialized in the wrong thread... would we add that overhead to all of them? Furthermore, that hampers inheritance from StatisticsGameState as now there is a new part of the contract to "update" that needs to be considered. If StatisticsGameState is not ready for inheritance now it should be declared final, but I don't like that too.

I use a different approach to state initialization (I add a "initialize" method) which works for me. I actually never create states from different threads but that's my preference anyway. Other than that, it is user's responsability to make sure that dependent resources are ready before creating a GameState or any other object.

In addition, I must say, all this comes from the evil of singletons :D

Nah, seriously, I don't believe these patches are a correct approach. I would like this "lazy initialization strategy embedded in the update method" become a normal solution for these kind of things.

Just tell people to use a GameTask to create that.

In any case, just my 2 cents...

hmm yeah its not very clean, but those NullPointerExceptions because something is not created in the OpenGL thread is one of the top 10 issues that come up in the forum.



Its confusing for a new User if he tries to create a DebugGameState and it throws a NPE at him.

this is fixed, DebugGameState can now be created outside of the OpenGL thread again.

Great. Thanks Core-Dump.