libgdx(Java)中重叠的NullPointerException

时间:2014-10-19 21:30:51

标签: java nullpointerexception libgdx

我正在制作一个简单的Java游戏作为我学习的一部分,当我运行它时,我得到了一个:

  

Exception in thread "LWJGL Application" java.lang.NullPointerException at com.mattihase.floatingpoint.GameScreen.generalUpdate(GameScreen.java:65) at com.mattihase.floatingpoint.GameScreen.render(GameScreen.java:42) at com.badlogic.gdx.Game.render(Game.java:46) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

这是它正在谈论的一般更新线以及它下面的界限:

if(pc.bounds.overlaps(gary.Rbounds))
    //where i test a bunch of booleans to see what the dialog returned should be
    //yes, i know i'm probably doing the ifs it unoptimized but i couldn't get it to
    //work the other way.
    //the pc.bounds stuff is there to make it follow the player character around the
    //world as the camera already does this.
      {
        if(pc.onquest = false){
            if(pc.finishquest = false){
            Assets.font.draw(batch, gary.Gdio, pc.bounds.x - 250, pc.bounds.y - 190);
            pc.onquest = true;
            }
        }
        if(pc.onquest = true){
            if(pc.finishquest = false){
            Assets.font.draw(batch, gary.GDdio, pc.bounds.x - 250, pc.bounds.y - 190);
            }
            }
        if(pc.haspoints = true){
            pc.finishquest = true;
        }
        if(pc.finishquest = true);
        Assets.font.draw(batch, gary.GCdio, pc.bounds.x - 250, pc.bounds.y - 190);
        pc.onquest = false;
        pc.award = true;
    }`

这些是玩家角色和npc的类(分别称为pc(类PlayerCharacter)和gary(类Rectoid)) 玩家角色

public Sprite image;
public Rectangle bounds;
public int score;
public String scoreprint;
public boolean onquest;
public boolean finishquest;
public boolean haspoints;
public boolean award;

public PlayerCharacter(){
    onquest = false;
    haspoints = false;
    finishquest = false;
    award = false;
    score = 0;
    image = Assets.s_pc;
    bounds = new Rectangle(256-16, 192-16, 16, 16);
    scoreprint = score + "";`

和加里    `public Sprite Rimage;     public Rectangle Rbounds;     public String Gdio;     public String GDdio;     public String GCdio;

public Rectoid()
{
    Rimage = Assets.s_rectoid;
    Rbounds = new Rectangle(1024, 64, 64, 32);
    Gdio = "the diolog";
    GDdio = "the diolog";
    GCdio = "the diolog";`

C

1 个答案:

答案 0 :(得分:1)

在Java中,要检查对象引用中的相等性或原始类型,必须使用==运算符。在if(pc.onquest = false){之类的行中,您要将false分配给onquest,而不是检查其值。 代码编译的原因是因为在Java中,赋值返回指定的值。 例如句子:

int zero = 0;
if (zero == 0){} //its true
if (zero = 0){} //compile error, becouse 'zero = 0' returns '0', the asigned value

您的代码中发生的是您正在使用布尔变量。

if(pc.bounds.overlaps(gary.Rbounds))
  {
    if(pc.onquest = false){ // HERE YOU ARE DOING TWO THINGS: assing false to 
                            // onquest and return false. The following code is never execute 
        if(pc.finishquest = false){ // SAME HERE
        Assets.font.draw(batch, gary.Gdio, pc.bounds.x - 250, pc.bounds.y - 190);
        pc.onquest = true;
        }
    }
    if(pc.onquest = true){// SAME HERE
        if(pc.finishquest = false){// SAME HERE
        Assets.font.draw(batch, gary.GDdio, pc.bounds.x - 250, pc.bounds.y - 190);
        }
        }
    if(pc.haspoints = true){// SAME HERE
        pc.finishquest = true;
    }
    if(pc.finishquest = true);// SAME HERE
    Assets.font.draw(batch, gary.GCdio, pc.bounds.x - 250, pc.bounds.y - 190);
    pc.onquest = false;
    pc.award = true;
}`

另外,要检查布尔值,只需使用它,不要将它与true或false进行比较,并使用!运算符。

只需简单使用if(!pc.onquest){如果您想检查是否禁用了onquest。

关于NullPointerEception,可能gary变量为null。检查您之前是否创建过它。它应该是这样的:

Rectoid gary = new Rectoid();