Home > Back-end >  How to avoid NullPointerException when initalizing ShapeRenderer at LibGdx
How to avoid NullPointerException when initalizing ShapeRenderer at LibGdx

Time:06-09

Could you please explain why I get NPE when initializing a ShapeRenderer? I try to get familiar with LibGdx, started at the basics, but I stuck at the beginning.

I have 2 classes which are:

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

public class MyGdxGame extends ApplicationAdapter {
    private ShapeRenderer shape;

    @Override
    public void create () {
        shape = new ShapeRenderer();
    }

    @Override
    public void render () {
        shape.begin(ShapeRenderer.ShapeType.Filled);
        shape.circle(50, 50, 50);
        shape.end();
    }
}

and the main class, including the entry point:

public class DesktopLauncher {
    public static void main(String[] args) {
        MyGdxGame game = new MyGdxGame();
        game.create();
        game.render();
    }
}

Unfortunately, I got NPE with the following error:

Exception in thread "main" java.lang.NullPointerException
    at com.badlogic.gdx.graphics.glutils.ShaderProgram.loadShader(ShaderProgram.java:204)
    at com.badlogic.gdx.graphics.glutils.ShaderProgram.compileShaders(ShaderProgram.java:183)
    at com.badlogic.gdx.graphics.glutils.ShaderProgram.<init>(ShaderProgram.java:166)
    at com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer20.createDefaultShader(ImmediateModeRenderer20.java:241)
    at com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer20.<init>(ImmediateModeRenderer20.java:55)
    at com.badlogic.gdx.graphics.glutils.ShapeRenderer.<init>(ShapeRenderer.java:116)
    at com.badlogic.gdx.graphics.glutils.ShapeRenderer.<init>(ShapeRenderer.java:111)
    at com.badlogic.gdx.graphics.glutils.ShapeRenderer.<init>(ShapeRenderer.java:107)
    at game.MyGdxGame.<init>(MyGdxGame.java:10)
    at game.DesktopLauncher.main(DesktopLauncher.java:6)

Process finished with exit code 1

One important thing, I'm using Maven, since I don't know much about Gradle, may it cause problem? Anyway, my Pom.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example.mylibgdx</groupId>
    <artifactId>mylibgdx</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <com.badlogicgames.gdx.version>1.11.0</com.badlogicgames.gdx.version>
    </properties>

    <repositories>
        <repository>
            <id>gdx-nightlies</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.badlogicgames.gdx</groupId>
            <artifactId>gdx</artifactId>
            <version>${com.badlogicgames.gdx.version}</version>
        </dependency>
    </dependencies>

</project>

Thank you for your help in advance!

CodePudding user response:

The problem is not Maven, but the way you start your application.
In your main method you create a game and start rendering while the libGdx features are not initialized yet. The usual way to start your application would look somewhat like this (for desktop applications):

public static void main (String[] arg) {
    Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
    config.setForegroundFPS(60);
    config.setTitle("My GDX Game");
    new Lwjgl3Application(new MyGdxGame(), config);
}

This way your game is created, but libGdx is responsible for calling the render method of you game class when the libGdx backend is ready.


If you download the libGdx setup tool (link to download) and create a project you can select the types of application you want your game to be runnable on (desktop, android, ios, html). The tool will generate a main class in each project, that starts the game correctly.

  • Related