有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

libgdx(Java)中重叠上的NullPointerException

作为学习的一部分,我正在制作一个简单的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;
    }`

这些是playercharacter和npc的类(分别称为pc(类playercharacter)和gary(类矩形) 玩家角色

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 + "";`

还有加里 `公共雪碧笠美; 公共基金; 公共字符串Gdio; 公共字符串GDdio; 公共字符串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) 个答案

  1. # 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进行比较,并使用!运算符

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

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

    Rectoid gary = new Rectoid();