Question

I am following a tutorial on creating a game with LibGdx from some ebook. the tutorial has steps for creating a game called "Canyon Bunny". Its a simple 2D game. but i keep getting this annoying error! (i also used to get the error on a different tutorial of the same genre)

i am in the early stages of the development for this game. and i am doing some test (of which i follow to the letter from the tutorial). I use a MAC and a i have tried many solutions with no luck at all.

Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.Adel.CanyonBunny.game.WorldUpdater.updateTestObjects(WorldUpdater.java:83)
at com.Adel.CanyonBunny.game.WorldUpdater.update(WorldUpdater.java:76)
at com.Adel.CanyonBunny.CanyonBunnyMain.render(CanyonBunnyMain.java:39)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

It is truly one of the most frustrating things a striving programmer can face. ill get the code of all the classes in case that's related somehow...

This is CanyonBunnyMain in the general program:

package com.Adel.CanyonBunny;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.Adel.CanyonBunny.game.*;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.Application;

public class CanyonBunnyMain implements ApplicationListener {
    private static final String TAG = CanyonBunnyMain.class.getName();
    private WorldUpdater worldUpdater;
    private WorldRenderer   worldRenderer ;
    private boolean paused ;
    public void create() { 
        //I'll set the log to debug for the developing process
        Gdx.app.setLogLevel(Application.LOG_DEBUG) ;
        worldUpdater = new WorldUpdater();
        worldRenderer = new WorldRenderer() ;

        // since, upon creation, the game is not paused, then:
        paused = false ;
    } 


    public void render() {

        if (paused = true) {
        //update the game by the time passed since the last update
        worldUpdater.update(Gdx.graphics.getDeltaTime()) ;
        }
        //sets the screen color to: CornFlower Blue
        Gdx.gl.glClearColor(0x64 / 255.0f, 0x95 / 255.0f, 0xed / 255.0f, 0xff / 255.0f);
        //clears the screen to prevent flickering
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT) ;

        //Render the game to the screen
        worldRenderer.render();
    }


    public void resize (int w, int h) { 
        worldRenderer.resize(w, h) ;
    } 
    public void pause () {
        paused = true ;
    }
    public void resume() { 
        paused = false ;
    }

    public void dispose() { 
        worldRenderer.dispose() ;
    }   }

this is the WorldRenderer (general program too) :

package com.Adel.CanyonBunny.game;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;

public class WorldRenderer {
    private OrthographicCamera cam;
    private SpriteBatch batch ;
    private WorldUpdater updater;

    public void WorldRenderer(WorldUpdater worldUpdater) { }

    public void init() { }


    public void render() { }

    public void resize(int w, int h) { }


    public void dispose() { }
}

this is the main class (from the desktop project: the one that i run on my MAC) :

package com.Adel.CanyonBunny;

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;

public class Main {
public static void main(String[] args) {
    LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
    cfg.title = "CanyonBunny";
    cfg.useGL20 = false;
    cfg.width = 800;
    cfg.height = 480;

    new LwjglApplication(new CanyonBunnyMain(), cfg);
}
 }

Any help will be wonderful. tell me should you need extra data

this is the WorldUpdater class for those who asked:

package com.Adel.CanyonBunny.game;

import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.MathUtils;

public class WorldUpdater {
    private final String TAG = WorldUpdater.class.getName();
    public Sprite[] testSprites;
    public int selectedSprite;
    public WorldRenderer worldRenderer;

    public void worldUpdater() {
        init() ;
    }

    public void init() {
        initTestObjects() ;
    }

    private void initTestObjects() {
        // create new array of 5 sprites
        testSprites = new Sprite[5] ;
        // Create empty POT-sized Pixmap with 8 bit RGBA pixel data
        int w = 32;
        int h = 32;

        Pixmap pixmap = createProceduralPixmap(w, h) ;

        //create a new texture from Pixmap data
        Texture texture = new Texture(pixmap) ;

        //create sprites using the just created texture
        for (int i = 0; i < testSprites.length; i++) {
            Sprite spr = new Sprite(texture) ;
            spr.setSize(1,1) ;

            //set origin to sprite's center
            spr.setOrigin(spr.getWidth() / 2.0f, spr.getHeight() / 2.0f) ;

            float randomX = MathUtils.random(-2.0f, 2.0f) ;
            float randomY = MathUtils.random(-2.0f, 2.0f) ;

            spr.setPosition(randomX, randomY) ;

            //put new sprite into array
            testSprites[i] = spr ;
        }
        //set first sprite as the selected one
        selectedSprite = 0 ;
    }

    private Pixmap createProceduralPixmap(int width, int height) {
        Pixmap pixmap = new Pixmap(width, height , Format.RGBA8888) ;
        //fill the square with red color at 50% opacity
        pixmap.setColor(1, 0, 0, 0.5f) ;
        pixmap.fill() ;

        //draw a yellow X in the pixmap
        pixmap.setColor(1, 1, 0 , 1) ;
        pixmap.drawLine(0, 0, width, height) ;
        pixmap.drawLine(width, 0, 0, height);

        //draw a cyan-colored border around the square
        pixmap.setColor(0, 1, 1, 1) ;
        pixmap.drawRectangle(0, 0, width, height) ;

        return pixmap;
    }



    public void update(float deltaTime) { 
        updateTestObjects(deltaTime);

    }

    private void updateTestObjects(float deltaTime) {
        //get current rotation from the selected sprite
         float rotation = testSprites[selectedSprite].getRotation();

        //rotate sprite by 90 degrees per second
        rotation += 90 * deltaTime;

        //wrap around at 360 degrees
        rotation %= 360 ;

        testSprites[selectedSprite].setRotation(rotation);
    }


}

Also, when i check this line out in Debugging mode:

testSprites = new Sprite[5] ;

"testSprites" keeps showing null. i hope this clears up some details! thanks again.

No correct solution

OTHER TIPS

The problem is with your "constructors", mainly in the updater (as the renderer does nothing):

public void worldUpdater() { ... }

Constructors should not specify return types - that's part of how the compiler recognizes them as constructors. As it is in your code, it's just a method you could call on an existing object instance. Change it like so:

public WorldUpdater() { ... }

Note the lack of a return type and the uppercase W.

You can change the renderer the same way. (But then you will have to pass the updater to its constructor in the main class.)

Also, Nine Magics is right that the way you store renderer and updater references in each other doesn't make much sense, even if it's not related to this problem. I see no reason why an updater class would need to know about its renderer, I'd remove that field.

In your WorldRenderer you specify this:

public void WorldRenderer(WorldUpdater worldUpdater) { }

And WorldRendere also carries an instance of an worldUpdater?

 private WorldUpdater updater;

But on your main file you create an instance of both renderer and updater?

       worldUpdater = new WorldUpdater();
    worldRenderer = new WorldRenderer() ;

I don't know, I might have tired eyes or something but this seems too complex. Can it be that you are refering to a wrong instance of WorldUpdater? Might edit this if I can wrap my head around it better.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top