引用另一个类时Libgdx屏幕NullPointerException异常 [英] Libgdx Screen NullPointerException when referencing another class

查看:159
本文介绍了引用另一个类时Libgdx屏幕NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每次点击谷歌的结果我能发现可能会帮助我,但没有成功。

I have clicked every Google result I could find that might help me with no success.

我建立了一个游戏,我想实现一个完整的UI。在我的游戏我有我的精灵单独的类,所以在做制作屏幕几个教程我想实现的东西,我可以用这些单独的类用于获取纹理,当前框架等。

I have built a game that I would like to implement a full UI with. In my game I have separate classes for my sprites, so while doing several tutorials for making screens I tried to implement something where I could use those separate classes for getting textures, current frames, etc.

我得到的地方,我可以在两个屏幕之间,如果他们不是指任何身外之物切换点,但每当我尝试引用的游戏画面在我的精灵类,它崩溃,指着我这个在游戏中类:

I have gotten to the point where I can switch between two screens if they are not referring to anything outside themselves, but whenever I try to reference my sprite class on the game screen, it crashes, pointing me to this in the game class:

if (screen != null) screen.render(Gdx.graphics.getDeltaTime());

因此​​,这里是code我使用(减去任何实现的方法或其他的东西,是默认的一类)。

So here is the code I am using (minus any implemented methods or other stuff that is default for a class).

Tutorial.java:

Tutorial.java:

public class Tutorial extends Game {
    MainMenuScreen mainMenuScreen ;
    AnotherScreen anotherScreen;
    Player player ;

    @Override
    public void create() {
        mainMenuScreen = new MainMenuScreen(this);
        anotherScreen = new AnotherScreen(this);
        setScreen(mainMenuScreen);
    }
}

MainMenuScreen.java(问题是在这里的批次):

MainMenuScreen.java (problem is here in the batch):

public class MainMenuScreen implements Screen {
    Tutorial game ;
    SpriteBatch batch ;
    Player player ;
    Texture test ;

    public MainMenuScreen(Tutorial game){
        this.game = game;
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

        batch.begin();
        batch.draw(player.playerTexture, 200, 200); // error points to here
        batch.end();
    }
}

AnotherScreen.java(运行正常):

AnotherScreen.java (runs fine):

public class AnotherScreen implements Screen {
    Tutorial game ;
    SpriteBatch batch ;
    Texture test ;

    public AnotherScreen(Tutorial game){
        this.game = game;
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

        batch.begin();
        batch.draw(test, 0, 200);
        batch.end();
    }

    @Override
    public void show() {
        batch = new SpriteBatch();
        test = new Texture(Gdx.files.internal("badlogic.jpg")); 
    }
}

Player.java:

Player.java:

public class Player {
    Texture playerTexture ;
    Vector2 position;
    String textureLoc;

    public Player(Vector2 position, String textureLoc){
    //What I am trying to get from AnotherScreen.java
        playerTexture = new Texture(Gdx.files.internal("badlogic.jpg")); 
    }
    public Texture getPlayerTexture() {
        return playerTexture;
    }
    public void setPlayerTexture(Texture playerTexture) {
        this.playerTexture = playerTexture;
    }
}

在控制台中确切的错误是:

The exact error in the console is:

Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.tutorial.MainMenuScreen.render(MainMenuScreen.java:25)
    at com.badlogic.gdx.Game.render(Game.java:46)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

我是什么做错了吗?我已经重新安排了code把一切在不同的地方,但我不能得到它的工作。感谢您的时间!

what am I doing wrong? I have rearranged the code putting everything in different places but I just can't get it to work. Thank you for your time!

我发现,如果我打开MainMenuScreen,然后去AnotherScreen并尝试从主得到一个纹理另一个显示,如果我有教程将它传递给另一个拿到前质感它的作品。这并不与球员的工作,虽然...

I have discovered that if I open MainMenuScreen, then go to AnotherScreen and try to get a texture from Main to show in Another, it works if I have Tutorial get the texture before passing it to Another. This does not work with Player though...

推荐答案

AnotherScreen的作品,因为你已经在使用它之前实例批次。然而,在MainMenuScreen,你没有实例化批处理和玩家的对象,因此,这个NPE之间。请确保您已实例化的批次和球员质地如下:

AnotherScreen works because you've instantiated batch before using it. However, in MainMenuScreen, you didn't instantiate both batch and the player object, ergo, the NPEs. Make sure you've instantiated the batch and player texture as follows:

public class MainMenuScreen implements Screen {
    Tutorial game ;
    SpriteBatch batch ;
    Player player ;
    Texture test ;

    public MainMenuScreen(Tutorial game){
        this.game = game;
    batch = new SpriteBatch();
    player = new Player(position, textureLoc); //modify arguments
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

        batch.begin();
        batch.draw(player.playerTexture, 200, 200); // error points to here
        batch.end();
    }
}

这篇关于引用另一个类时Libgdx屏幕NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆