Search code examples
javaeclipseslick2djarsplice

NullPointerException on exported Slick2D project


My Slick2D project seems to run okay when executed in the Eclipse IDE, but when I export it through JarSplice, it throws a NullPointerException in the class 'Menu'.

Here is the snippet of code that throws the error:

@Override
public void render( GameContainer gc, StateBasedGame sbg, Graphics g ) throws SlickException {

    g.setColor( Color.black );
    g.setFont( Options.getGameFont() );

    //  Draw foreground objects.
    {
        g.drawString( "Start", 10,  gc.getHeight()-50 );   // This line here
    }
}

Along with the stack trace:

Sat Apr 18 17:20:59 BST 2015 ERROR:null
java.lang.NullPointerException
    at org.newdawn.slick.Graphics.drawString(Graphics.java:1366)
    at com.mgthomas.states.Menu.render(Menu.java:48)
    at org.newdawn.slick.state.StateBasedGame.render(StateBasedGame.java:199)
    at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:688)
    at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
    at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
    at com.mgthomas.Launch.main(Launch.java:48)
Sat Apr 18 17:20:59 BST 2015 ERROR:Game.render() failure - check the game code.
org.newdawn.slick.SlickException: Game.render() failure - check the game code.
    at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:691)
    at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
    at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
    at com.mgthomas.Launch.main(Launch.java:48)

I have already added 'slick.jar' and 'lwjgl.jar', as well as all of the windows natives, to JarSplice, and it exports without any problems. However, when the exported .jar file is ran through the command prompt, the error above is displayed.


Solution

  • (Sorry - I don't have enough reputation points to post a comment so am posting this as an answer.)

    If you have added the library jars and natives as per the guide below then it should work.

    Simple guide to creating a .jar, .exe or .jnlp?

    Which version of Slick2D are you using? I only ask because I recently updated my libraries to the Slick build 237 with LWJGL 2.9.2 version (10 January 2015) and had some issues with it. I reverted to the Slick build 237 with LWJGL 2.9.0 (3 June 2013) in the end. Even though the build numbers are the same, the issues I had suggested that something had changed in Slick2D and made it unstable. I've noted online that others have had problems with that build as well.