Search code examples
javanullpointerexceptioncompilationlibgdx

LibGdx - NullPointerException for the default code


I get these errors while compiling the default LibGdx code on my laptop. Somehow the exact same thing works on my PC.

Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.backends.lwjgl.LwjglGraphics.createDisplayPixelFormat(LwjglGraphics.java:321)
at com.badlogic.gdx.backends.lwjgl.LwjglGraphics.setupDisplay(LwjglGraphics.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:142)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)

Used the gdx setup application, generated the project for desktop only. I've added a configuration so that I can run it. And during the compiling I get this error. I haven't changed anything in the default code. Why is this happening?

public class DesktopLauncher {
    public static void main (String[] arg) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        new LwjglApplication(new MyGdxGame(), config);
    }
}

public class MyGdxGame extends ApplicationAdapter {
    SpriteBatch batch;
    Texture img;

    @Override
    public void create () {
        batch = new SpriteBatch();
        img = new Texture("badlogic.jpg");
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw(img, 0, 0);
        batch.end();
    }

    @Override
    public void dispose () {
        batch.dispose();
        img.dispose();
    }
}

The LwjglGraphics line in which the exception occurs is:

throw new GdxRuntimeException("OpenGL is not supported by the video driver: " + glVersion.getDebugVersionString(), ex3);

Solution

  • Put this code into desktop launcher class.

    This will allow libgdx to run as software openGL mode.

     System.setProperty("org.lwjgl.opengl.Display.allowSoftwareOpenGL", "true"); 
    

    Your code should look like this

    public class Main {
       public static void main(String[] args) {
          LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
          System.setProperty("org.lwjgl.opengl.Display.allowSoftwareOpenGL", "true");
          config.title = "Mygame"; 
          config.width = 1920;
          config.height = 1080;
          new LwjglApplication(new MyGame(), config);
       }
    }
    

    Also update your graphic drivers.