【问题标题】:NullPointerExceptions when initializing a RevoluteJointDef初始化 RevoluteJointDef 时出现 NullPointerExceptions
【发布时间】:2014-07-17 00:22:08
【问题描述】:

我已经开始深入研究 AndEngine 的更复杂用途,因为我开始掌握它的窍门,或者我是这么认为的。我遇到了一个我似乎无法弄清楚的 NPE,因此我将不胜感激。

下面的类代表一个粗制滥造的布娃娃/火柴人,该类本身使用andengine注册了它的所有纹理、身体、关节等等。 mainActivity(扩展了 SimpleBaseGameActivity)然后在初始化期间以这种方式调用其构造函数:

this.skater = new Skater(this, mPhysicsWorld, new Vector2(startX, startY));

我已经删除了一些代码,这些代码主要是对其他关节和四肢重复相同的内容,因此只包括踝关节的内容,因为文本墙已经足够大了。

NPE 被抛出到构造函数末尾的这一行:

this.jAnkle.initialize(bSkates, bLegs, new Vector2(stickmanPos.x, sSkates.getHeight()));

public class Stickman {

        //////////////
        // Constructor

        public Stickman(Testgame game, PhysicsWorld engine, Vector2 stickmanPos) {

                this.game = game;
                this.engine = engine;
                this.stickmanPos = stickmanPos;    

                // Create textures
                textureAtlas = new BitmapTextureAtlas(game.getTextureManager(), 1024, 1024, TextureOptions.BILINEAR);
                this.tSkates   = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.textureAtlas, this.game, "stickman_skates.png",   0, 0, 2, 1);
                this.tLegs   = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.textureAtlas, this.game, "stickman_legs.png",   0, 0, 2, 1);
                // --SNIPPED--

                // Create sprites
                this.sSkates   = new AnimatedSprite(stickmanPos.x,              stickmanPos.y, this.tSkates, game.getVertexBufferObjectManager());
                this.sLegs   = new AnimatedSprite(stickmanPos.x + skatesToLegs, stickmanPos.y, this.tLegs,   game.getVertexBufferObjectManager());
                // --SNIPPED--

                // Create body with sprites
                this.bSkates   = PhysicsFactory.createBoxBody(engine, sSkates,   BodyType.DynamicBody, game.FIX_SKATER);
                this.bLegs   = PhysicsFactory.createBoxBody(engine, sLegs,   BodyType.DynamicBody, game.FIX_SKATER);
                // --SNIPPED--         

                // Register bodies with Box2D
                this.engine.registerPhysicsConnector(new PhysicsConnector(this.sSkates,   this.bSkates,   true, true));
                this.engine.registerPhysicsConnector(new PhysicsConnector(this.sLegs,   this.bLegs,   true, true));

                // --SNIPPED--         

                // Create joints
                RevoluteJointDef jAnkle = new RevoluteJointDef();
                System.out.println("aoeu aoeu aoeu aoeu");
                this.jAnkle.initialize(bSkates, bLegs, new Vector2(stickmanPos.x, sSkates.getHeight()));
                this.jAnkle.enableMotor = true;
                this.jAnkle.motorSpeed = this.dex;
                this.jAnkle.maxMotorTorque = this.str;

        }

        ////////////////       
        // Fields
        private Testgame game;
        private PhysicsWorld engine;
        private Vector2 stickmanPos;

        // Config
        private float skatesToLegs   = 1;
        private float legsToThigh  = 1;
        private float thighToTorso = 1;
        private float torsoToHead  = 1;
        private float torsoToArms  = 1;

        // Stats
        private float gli = 0;  // Higher number = lower skate friction
        private float str = 0;  // Joint torque
        private float dex = 0;  // Joint speed
        private float con = 0;  // Impact tolerance

        // JointPositions
        private float shoulderPos;
        private float neckPos;
        private float hipPos;
        private float kneePos;

        protected BitmapTextureAtlas  textureAtlas;

        protected ITiledTextureRegion tSkates;
        protected ITiledTextureRegion tLegs;
        // --SNIPPED--

        protected AnimatedSprite sSkates;
        protected AnimatedSprite sLegs;
        // --SNIPPED--

        protected Body bSkates;
        protected Body bLegs;
        // --SNIPPED-- 

        protected RevoluteJointDef jAnkle;
        // --SNIPPED-- 
}

希望这不是我所做的明显愚蠢的事情。如果是,请见谅。

【问题讨论】:

  • 我可以看到您正在隐藏 jAnkle 变量,但我看不出这是如何导致您的 NPE 被抛出的。
  • @HovercraftFullOfEels 你说的阴影是什么意思
  • 可能火柴人对象为空
  • 你已经在protected RevoluteJointDef jAnkle;中声明了变量,但在构造函数中重新声明了它。
  • @JunedAhsan:???这是 Stickman 构造函数。

标签: java android nullpointerexception andengine


【解决方案1】:

您似乎隐藏了几个变量,包括您的 sSkates、bSkates、jAnkle 变量。这是您在类中以及在构造函数、方法或其他本地块中再次声明变量的地方。我看不出这是如何导致您的 NPE 在构造函数内,但它是其他地方 NPE 的常见原因。

【讨论】:

  • 这是问题的原因。只有本地化到构造函数的 jAnkle 被赋予了一个正确实例化的对象,而在代码后面使用的那个被保留为 Null。删除了构造函数中多余的RevoluteJointDef,它就像一个魅力。
  • @Jarmund:让我对您的问题感到困惑的是,您将 NPE 本地化为 从构造函数中抛出,这是没有意义的。它应该在构造函数之外抛出这种类型的错误。
  • 我的猜测是它的发生是因为我倾向于过度使用 this 关键字,并且我认为这使它在类中的 jAnkle 字段上运行,而不用打扰构造函数本地化等效项。
  • @Jarmund:天哪!当然你是对的!感谢您指出这一点!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-14
相关资源
最近更新 更多