Java 初始化RevolutionJointDef时的NullPointerException
我已经开始钻研AndEngine更复杂的用途,因为我开始掌握它的窍门,或者我是这么想的。我遇到了一个我似乎无法理解的NPE,因此我将非常感谢任何帮助 下面的类表示一个粗糙的碎布玩偶/粘贴工,该类本身使用andengine注册其所有纹理、身体、关节等。mainActivity(扩展SimpleBaseGameActivity)然后在初始化过程中以以下方式调用其构造函数:Java 初始化RevolutionJointDef时的NullPointerException,java,android,nullpointerexception,andengine,Java,Android,Nullpointerexception,Andengine,我已经开始钻研AndEngine更复杂的用途,因为我开始掌握它的窍门,或者我是这么想的。我遇到了一个我似乎无法理解的NPE,因此我将非常感谢任何帮助 下面的类表示一个粗糙的碎布玩偶/粘贴工,该类本身使用andengine注册其所有纹理、身体、关节等。mainActivity(扩展SimpleBaseGameActivity)然后在初始化过程中以以下方式调用其构造函数: this.skater=新滑板手(this,mpphysisworld,newvector2(startX,startY)) 我
this.skater=新滑板手(this,mpphysisworld,newvector2(startX,startY))代码>
我删掉了一些代码,这些代码主要是对其他关节和肢体重复相同的内容,因此只包括踝关节的内容,因为文本墙足够大
NPE在构造函数末尾的这一行抛出:
this.jAnkle.initialize(bSkates、bLegs、newvector2(stickmanPos.x、sSkates.getHeight())代码>
希望我所做的事情不是很愚蠢。如果是,请原谅。您似乎在跟踪几个变量,包括您的sSkates、bSkates和jAnkle变量。在这里,您可以在类中声明变量,也可以在构造函数、方法或其他局部块中声明变量。我看不出这是如何导致构造函数中的NPE,但这是其他地方NPE的常见原因。我可以看到你在跟踪jAnkle变量,但是我看不出这是如何导致你的NPE被抛出的。@HoverCraftFullOfels你说的阴影是什么意思也许stickman对象为空你已经在类中声明了变量代码>,然后在构造函数中重新声明。@JunedAhsan:????这是Stickman构造函数。这是问题的原因。只有本地化到构造函数的jAnkle被赋予了一个正确的实例化对象,并且代码后面正在使用的对象被保留为Null。删除了构造函数中多余的RevolutionJointDef
,它像一个符咒一样工作。@Jarmund:你的问题让我困惑的是,你将NPE本地化为从构造函数中抛出,这没有意义。它应该带着这种类型的错误被抛出构造函数之外。我猜这是因为我倾向于过度使用this
关键字,我认为这使它在类中的jAnkle字段上运行,而不必麻烦构造函数本地化的等价项。@Jarmund:D'oh!当然你是对的!谢谢你指出这一点!
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--
}