初始化RevoluteJointDef时的NullPointerExceptions

时间:2014-07-17 00:22:08

标签: java android nullpointerexception andengine

我开始潜入AndEngine的更复杂的用途,因为我开始掌握它,或者我想。我遇到了一个我似乎无法弄清楚的NPE,所以我很感激你的帮助。

下面的类代表一个粗糙的布娃娃/火柴人,而且这个类本身用andengine记录它的所有纹理,身体,关节和诸如此类的东西。 mainActivity(扩展SimpleBaseGameActivity)然后在初始化期间以这种方式调用其构造函数:

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

我已经修剪了一些代码,这些代码主要是针对其他关节和四肢重复相同的东西,因此只包含踝关节的东西,因为文本墙足够大。 / p>

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-- 
}

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

1 个答案:

答案 0 :(得分:1)

您似乎正在遮蔽几个变量,包括sSkates,bSkates,jAnkle变量。这是您在类中声明变量的地方,再次在构造函数,方法或其他本地块中声明变量。我不知道这是如何导致您的NPE 在构造函数中,但这是NPE在其他地方的常见原因。