2014-07-17 2 views
1

AndEngine에 대한 더 복잡한 용도로 다이빙을 시작했습니다. 멈추기 시작 했으므로 생각했습니다. 나는 알아낼 수없는 NPE를 만났기 때문에 어떤 도움을 주셔서 감사하겠습니다.RevoluteJointDef를 초기화 할 때의 NullPointerExceptions

아래의 클래스는 원유 래그 돌/stickman을 나타내며 클래스 자체는 모든 텍스처, 바디, 조인트 및 기타 등 록을 andengine에 등록합니다. 에 대한 그래서 유일한 물건,

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

나는 대부분의 다른 관절과 팔다리에 같은 물건을 반복되는 몇 가지 코드를 멀리 손질했습니다 (SimpleBaseGameActivity를 확장)를 mainActivity는 초기화하는 동안 이런 식으로 생성자를 호출 발목 관절은 텍스트의 벽이 그대로 충분히 크므로 포함됩니다.

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

가 잘하면 내가했던 것을 분명히 멍청한 짓이 아니다. 그렇다면 용서해주세요.

+0

jAnkle 변수가 섀도 잉되어있는 것을 볼 수는 있지만 NPE가 어떻게 발생하는지 알 수 없습니다. –

+0

@HovercraftFullOfEels 섀도 잉이란 무엇을 의미합니까? – Jarmund

+0

stickman 객체가 null 일 수도 있습니다. –

답변

1

sSkates, bSkates, jAnkle 변수를 포함하여 여러 변수를 섀도 잉하는 것처럼 보입니다. 여기서 클래스에서 변수를 선언하고 생성자, 메소드 또는 다른 로컬 블록에서 다시 한 번 변수를 선언합니다. 이것이 NPE를 어떻게 유발하는지 알지 못합니다 안에 생성자가 있지만 NPE의 다른 원인은 그 원인이 아닙니다.

+0

이것은 문제의 원인이었다. 생성자에 지역화 된 jAnkle 만 제대로 인스턴스화 된 객체를 제공 받았고 사용중인 코드는 Null로 남았습니다. 생성자에서 불필요한'RevoluteJointDef'를 제거하여 매력처럼 작동했습니다. – Jarmund

+0

@ 자문 : 질문에 대해 혼란 스러웠던 것은 NPE를 생성자 *에서 던져진 것으로 현지화했기 때문에 * 이해가되지 않습니다. 이 유형의 오류가있는 생성자 외부에서 발생해야합니다. –

+0

제 생각에 필자는'this' 키워드를 과도하게 사용하는 경향이 있었기 때문에 발생했습니다. 클래스의 jAnkle 필드에서 작동하게하고 생성자의 지역화 된 동등 물을 사용하지 않아도된다고 생각합니다. – Jarmund