2

我非常确定,命名一个对象并不会改变它的任何东西

A a = new A();
A b = new A();

第二个对象应该具有与第一个对象完全相同的属性。所以这是我的编码问题

public class Animation {

public static final Animation MOROTE_SEOI_NAGE = new Animation(JudokaComponent.MOROTE1, JudokaComponent.MOROTE2, JudokaComponent.MOROTE3);
public static final Animation morote_seoi_nage = new Animation(JudokaComponent.judokaStanding1, JudokaComponent.judokaStanding2, JudokaComponent.judokaStanding1);

public static final Animation UCHI_MATA = new Animation(JudokaComponent.uchiMata1, JudokaComponent.uchiMata2, JudokaComponent.uchiMata3);
public static final Animation uchi_mata = new Animation(JudokaComponent.judokaStanding1, JudokaComponent.judokaStanding2, JudokaComponent.judokaStanding1);

public static final Animation O_SOTO_GARI = new Animation(JudokaComponent.oSotoGari1, JudokaComponent.oSotoGari2, JudokaComponent.oSotoGari3);
public static final Animation Lel = new Animation(JudokaComponent.oSotoGari1, JudokaComponent.oSotoGari2, JudokaComponent.oSotoGari3);
public static final Animation o_soto_gari = new Animation(JudokaComponent.judokaStanding1, JudokaComponent.judokaStanding2, JudokaComponent.judokaStanding1);


public int ID;
public BufferedImage[] SPRITES;

public Animation(BufferedImage step1, BufferedImage step2, BufferedImage step3){
    if(step1 == null || step2 == null || step3 == null) {
        if(step1 == null) System.err.print("Step 1 was NPE. Address: " + this + "\n");
        if(step2 == null) System.err.print("Step 2 was NPE. Address: " + this + "\n");
        if(step3 == null) System.err.print("Step 3 was NPE. Address: " + this + "\n");
        throw new RuntimeException("Animation NEP");
    }
    BufferedImage[] SPRITES = {step1, step2, step3};
    this.SPRITES = SPRITES;
}


}

更具体地说,这里的这些行:

public static final Animation O_SOTO_GARI = new Animation(JudokaComponent.oSotoGari1, JudokaComponent.oSotoGari2, JudokaComponent.oSotoGari3);
public static final Animation Lel = new Animation(JudokaComponent.oSotoGari1, JudokaComponent.oSotoGari2, JudokaComponent.oSotoGari3);

对我来说,它们看起来就像完全相同的对象,但名称不同。当我在没有 O_SOTO_GARI 对象的情况下运行代码时,它按预期工作,至少启动。但是,一旦我取消注释 O_SOTO GARI 对象创建,我会在游戏开始前得到以下输出:

Step 1 was NPE. Address: com.kruhlmann.judoka.graphics.Animation@184ec44
Step 2 was NPE. Address: com.kruhlmann.judoka.graphics.Animation@184ec44
Step 3 was NPE. Address: com.kruhlmann.judoka.graphics.Animation@184ec44
Exception in thread "main" java.lang.ExceptionInInitializerError

但为什么?如果我制作完全相同的对象并将其称为“Lel”,则不会出现此错误。为什么会这样,我很困惑

4

0 回答 0