0

(If my english is wrong, i'm sorry, i'm brazilian.)

I'm newbie at Libgdx and I found a error while trying to create my game. I'm using the book "Learning Libgdx Game Development" and I have a error in my project.

The error is:

Exception in thread "LWJGL Application" java.lang.NullPointerException at com.keerp.game.WorldRenderer.renderTestObjects(WorldRenderer.java:43) at com.keerp.game.WorldRenderer.render(WorldRenderer.java:35) at com.keerp.game.MainClass.render(MainClass.java:62) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:223) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)

Here's my codes:

MainClass:

    package com.keerp.game;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
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.badlogic.gdx.ApplicationListener;

public class MainClass implements ApplicationListener {

    private static final String TAG = MainClass.class.getName();

    private WorldController worldController;
    private WorldRenderer worldRenderer;

    private boolean paused;


    @Override
    public void create () 
    {
        // set libgdx log level to DEBUG    
        Gdx.app.setLogLevel(Application.LOG_DEBUG);

        // initialize controller and renderer
        worldController = new WorldController();
        worldRenderer = new WorldRenderer(worldController);

        // game world is active on start
        paused = false;

    }

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


    @Override
    public void render () {

        if(!paused)
        {
            worldController.update(Gdx.graphics.getDeltaTime());
        }

        // setting screen color
        Gdx.gl.glClearColor(0x64/255.0f, 0x95/255.0f, 0xed/255.0f, 0xff/255.0f);

        // clear the screen
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // render game world to screen
        worldRenderer.render();
    }

    @Override
    public void resize(int width, int height)
    {
        worldRenderer.resize(width, height);
    }

    @Override
    public void pause()
    {
        paused = true;
    }

    @Override
    public void resume()
    {
        paused = false;
    }
}

Constants:

package com.keerp.game;

public class Constants {

    // setting constants tag
    private static final String TAG = Constants.class.getName();

    public static final float VIEWPORT_WIDTH = 5.0f;

    public static final float VIEWPORT_HEIGHT = 5.0f;

}

WorldController:

package com.keerp.game;

import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.g2d.Sprite;
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 WorldController {

    private static final String TAG = WorldController.class.getName();

    public Sprite[] testSprites;
    public int selectedSprite;


    public WorldController()
    {
        init();
    }

    private void initTestObjects()
    {
        testSprites = new Sprite[5];

        int width = 32;
        int height = 32;

        Pixmap pixmap = createProceduralPixmap(width, height);

        Texture texture = new Texture(pixmap);

        for(int i = 0; i < testSprites.length; i++)
        {
            Sprite spr = new Sprite(texture);
            spr.setSize(1, 1);
            spr.setOrigin(spr.getWidth() / 2.0f, spr.getHeight() / 2.0f);

            // calculating random position for each sprite

            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;

        }

        selectedSprite = 0;
    }


    private Pixmap createProceduralPixmap(int width, int height)
    {
        Pixmap pixmap = new Pixmap(width, height, Format.RGBA8888);

        // fill square with red color at 50% opacity
        pixmap.setColor(1, 0, 0, 0.5f);
        pixmap.fill();

        // draw a yellow-colored X shape on square
        pixmap.setColor(1, 1, 0, 1);
        pixmap.drawLine(0, 0, width, height);
        pixmap.drawLine(width, 0, 0, height);

        // draw a cyan-colored border around square

        pixmap.setColor(0, 1, 1, 1);
        pixmap.drawRectangle(0, 0, width, height);


        return pixmap;
    }

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

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

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

        // set new rotation value to selected sprite
        testSprites[selectedSprite].setRotation(rotation);


    }

    private void init()
    {

    }

    public void update(float deltaTime)
    {

    }

}

WorldRenderer:

package com.keerp.game;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Disposable;

public class WorldRenderer implements Disposable 
{

    private static final String TAG = WorldRenderer.class.getName();

    private OrthographicCamera camera;
    private SpriteBatch batch;
    private WorldController worldController;


    public WorldRenderer(WorldController worldController)
    {
        this.worldController = worldController;
        init();
    }

    private void init()
    {
        batch = new SpriteBatch();
        camera = new OrthographicCamera(Constants.VIEWPORT_WIDTH, Constants.VIEWPORT_HEIGHT);
        camera.position.set(0, 0, 0);
        camera.update();
    }


    public void render()
    {
        renderTestObjects();
    }

    private void renderTestObjects()
    {
        batch.setProjectionMatrix(camera.combined);
        batch.begin();

        for(Sprite sprite : worldController.testSprites)
            sprite.draw(batch);

        batch.end();
    }

    public void resize(int width, int height)
    {
        camera.viewportWidth = (Constants.VIEWPORT_HEIGHT / height) * width;
        camera.update();
    }

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

I don't know if it's a easy error to resolve, but I really want help.

Thanks!

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
kibe
  • 163
  • 1
  • 13

1 Answers1

1

The initTestObjects() of WorldController class is defines and initializes the member variable-Sprite[] testSprites. But it is not called anywhere. You are getting NullPointerException in line -44 of WorldRenderer.java because the testSprites vairable is null and you are trying to iterate it for its contents.

Please include initTestObjects() code in WorldController.class.

 private void init()
{

initTestObjects(); }