App Variable Null?

I’m trying to export/deploy a simple test project as an Applet, but when I run it (in the browser, it runs fine from the jar/jdk) it gives me this error (in the java console):

Code:
Exception in thread "LWJGL Renderer Thread" java.lang.NullPointerException at client.Main.simpleInitApp(Main.java:44) at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:231) at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:129) at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:205) at java.lang.Thread.run(Unknown Source)

This is the Main.java:
Code:
public class Main extends SimpleApplication {
static AppSettings settings = new AppSettings(true);
static Main appref;
static LundWidget lund = new LundWidget();
static ArrayList<Tile> tiles = new ArrayList();
static MouseControl mouse = new MouseControl();
boolean doSetup = true;

public static void main(String[] args)
{
    Main app = new Main();
    settings.setTitle("Lund - The Indecent RTS");
    settings.setWidth(800);
    settings.setHeight(600);
    app.setShowSettings(false);
    app.setSettings(settings);
    app.start();
    appref = app;
}

@Override
public void simpleInitApp()
{
    int x;
    int y = 0;
    flyCam.setEnabled(false);
    DirectionalLight light = new DirectionalLight();
    light.setDirection(new Vector3f(-10,-10,-10));
    light.setColor(ColorRGBA.White);
    appref.guiNode.addLight(light);
    for (int g = 0; g < 10; g++)
    {
        x = 0;
        for (int i = 0; i < 13; i++)
        {
            Tile temp = new Tile(Tile.TileType.GRASS, new Vector2f(x, y));
            temp.createTile();
            x += 64;
        }
        y += 64;
    }
}

@Override
public void simpleUpdate(float tpf)
{
    if(doSetup)
    {
        mouse.bindKeys(appref);
        doSetup = false;
    }
}

@Override
public void simpleRender(RenderManager rm)
{
    //TODO: add render code
}

}


The error refers to "appref.guiNode.addLight(light);", but changes to whatever the first reference of appref is. I'm unsure of why it does this in the applet form but not the regular form, as I'm new to applets and not sure if they function differently.

appref is null when you’re in simpleInitApp(), because you’re in app.start() thus, appref = app; hasn’t been processed yet.

1 Like

Also, learn to use the debug tools.



F7, F8, breakpoints, etc. That’s an easy one to catch.

1 Like

Hmph.



Just changed it to:

Code:
public static void main(String[] args) { Main app = new Main(); settings.setTitle("Lund - The Indecent RTS"); settings.setWidth(800); settings.setHeight(600); app.setShowSettings(false); app.setSettings(settings); appref = app; app.start(); }

and the same error occurs. That WAS the way to fix it... right? :crickets:.

The applet won’t be running main(). You shouldn’t be doing anything “real” in there because of that.



If you want to keep a static reference for some reason then you need to set it in simpleInit()… other things won’t be able to get anything useful from it until then anyway. Might as well not give them access until it’s ready.



And the only reason this works in non-applet mode is because there is a slight delay between when app.start() is called and simpleInit() gets run on the render thread. On a single core system, it’s technically possible that simpleInit() would get run even before app.start() returns and it would then fail there too.

1 Like

Thank you very much guys, setting it outside of the block did it. Once again pspeed saves the day :).

@koaandl said:
Thank you very much guys, setting it outside of the block did it. Once again pspeed saves the day :).


What he did is spoon-fed you. :P
1 Like

Bah, sometimes daft individuals like me need a little spoon feeding now and then to learn how to improve themselves :).