线程“LWJGL Application”中的异常java.lang.NullPointerException

时间:2014-02-23 16:52:53

标签: java opengl nullpointerexception cross-platform libgdx

我正在阅读一些关于使用某些电子书创建LibGdx游戏的教程。该教程包含创建名为“Canyon Bunny”的游戏的步骤。它是一个简单的2D游戏。但我不断收到这个恼人的错误! (我也常常在同一类型的不同教程上得到错误)

我正处于此游戏开发的早期阶段。我正在做一些测试(其中我遵循教程中的字母)。我使用MAC而且我已经尝试了许多解决方案而根本没有运气。

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)

这是一个努力奋斗的程序员可能面临的最令人沮丧的事情之一。 如果以某种方式相关的话,得到所有类的代码......

这是一般程序中的CanyonBunnyMain:

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() ;
    }   }

这是WorldRenderer(通用程序):

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() { }
}

这是主类(来自桌面项目:我在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);
}
 }

任何帮助都会很精彩。 告诉我你是否需要额外的数据

对于那些问过的人来说,这是WorldUpdater课程:

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);
    }


}

另外,当我在调试模式中检查此行时:

testSprites = new Sprite[5] ;

“testSprites”一直显示为null。 我希望这能清除一些细节! 再次感谢。

2 个答案:

答案 0 :(得分:1)

问题在于你的&#34;构造函数&#34;,主要在更新程序中(因为渲染器什么都不做):

public void worldUpdater() { ... }

构造函数不应指定返回类型 - 这是编译器如何将它们识别为构造函数的一部分。就像在您的代码中一样,它只是您可以在现有对象实例上调用的方法。像这样改变:

public WorldUpdater() { ... }

请注意缺少返回类型和大写W。

您可以采用相同的方式更改渲染器。 (但是你必须将更新程序传递给主类中的构造函数。)

另外,Nine Magics是正确的,即使它与此问题无关,将渲染器和更新程序引用存储在彼此中的方式也没有多大意义。我认为没有理由为什么更新程序类需要知道它的渲染器,我删除了该字段。

答案 1 :(得分:0)

在WorldRenderer中指定:

public void WorldRenderer(WorldUpdater worldUpdater) { }

WorldRendere还带有一个worldUpdater的实例?

 private WorldUpdater updater;

但是在您的主文件中,您创建了渲染器和更新程序的实例?

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

我不知道,我可能已经厌倦了眼睛或其他东西,但这似乎太复杂了。您是否正在引用WorldUpdater的错误实例?如果我可以更好地包围它,可能会编辑它。