带参数的新对象会引发nullpointerexception

时间:2014-08-14 07:28:36

标签: java android object parameters nullpointerexception

我目前正在使用Libgdx进行AndroidDesktop开发,但遇到了一些麻烦。 (没有具体的Libgdx)。这只是一个用户错误(我已经尝试解决这个问题2个小时了,而且我完全难倒)。所以这是最新的:我创建了一个类,所以我可以简单地使用一个字符串来加载Texture(用作spritesheet)

public class Animator{

private int    FRAME_Y;     
private int    FRAME_X;     

private Animation           walk_down, walk_left, walk_right, walk_up, walk_spin;
private Animation MainAnimation;      
private static Texture             Sheet;      
static TextureRegion[]         walkRegion_down, walkRegion_left, walkRegion_right, walkRegion_up, walkRegion_spin;     
private SpriteBatch         spriteBatch;        
static TextureRegion           currentFrame;       

static float stateTime;   
private TextureRegion test;
private TextureRegion MainRegion;

public Animator(String location, int Y, int X, boolean flipped) {
    this.FRAME_Y = Y;
    this.FRAME_X = X;
    Sheet = new Texture(Gdx.files.internal(location));

    int frameWidth = Sheet.getWidth() / FRAME_Y;
    int frameHeight = Sheet.getHeight() / FRAME_X;


    TextureRegion[][] tmp = TextureRegion.split(Sheet, frameWidth, frameHeight);
    MainRegion = new TextureRegion(Sheet, 0, 0, frameWidth, frameHeight);
    MainAnimation = new Animation(0.2f, MainRegion, MainRegion);
    spriteBatch = new SpriteBatch();                
    stateTime = 0f;
    return;
}

public static void render(Animator G, float x, float y) {
    SpriteBatch spriteBatch1 = new SpriteBatch();
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);                        
    stateTime += Gdx.graphics.getDeltaTime();
    currentFrame = G.MainAnimation.getKeyFrame(stateTime);  

    spriteBatch1.begin();
    spriteBatch1.draw(currentFrame, x, y);       
    spriteBatch1.end();
}}

这个函数中正在使用这两个函数来创建新的对象

public class LetSurviveMain extends Game{

//public static Entity player1;
public static Animator test, test2;
@Override
public void create () {

    //Map = new TmxMapLoader().load("Tutorial.tmx");
    //GameStart.show();
    Animator test = new Animator("Animations/test.png", 4, 4, false);
    Animator test2 = new Animator("Animations/test2.png", 4, 4, false);
}



@Override
public void render () {
    //GameStart.render();
    Animator.render(test, 150, 150);
    Animator.render(test2, 250, 200);
}}

确切的控制台错误是:

  

线程中的异常" LWJGL应用程序"显示java.lang.NullPointerException       在com.tlog.evil.Animator.render(Animator.java:71)       在com.tlog.evil.LetSurviveMain.render(LetSurviveMain.java:24)       在com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)       在com.badlogic.gdx.backends.lwjgl.LwjglApplication $ 1.run(LwjglApplication.java:114)

它指向我的Animator课程中的第71行,即:

  currentFrame = G.MainAnimation.getKeyFrame(stateTime);

我知道这将是一个简单的解决方案(只是我思考它)这是我做的: 刚删除Animator在分配前面,因为我已经在课堂上创建了一个。 (渲染方法指的是从未设置过的东西。

谢谢你们!

0 个答案:

没有答案