So I’m having an issue with adding to an ArrayList or a Linked List (tried both, but each crash the same). I’m working off of an AndEngine tutorial (Jimvaders, which worked fine), but when adapting it to my own project, it isn’t working properly. Basically, when I shoot a bullet, it gets added to a list of bullets, but in my project, trying to touch the ArrayList or LinkedList, located in the GameScene, from the playerChar class causes the whole game to crash. I haven’t even done anything with the list yet, so it’s just the act of adding my PlayerBullet to the list that is causing the problem as far as I can tell.
GameScene:
public ArrayList<PlayerBullet> bulletList;
in my PlayerChar class
public void shoot(int playerFacing) { //TODO
GameScene scene = (GameScene) BaseActivity.getSharedInstance().getCurrentScene();
float shootX = 2;
PlayerBullet b =(PlayerBullet)PlayerBulletPool.sharedBulletPool().obtainPoolItem();
if (playerFacing == -1){
shootX *= -1;
}
else{
shootX += this.getWidth();
}
b.sprite.setPosition(this.getX() + shootX, this.getY()+(this.getHeight()/2));
MoveXModifier mod = new MoveXModifier(0.5f, b.sprite.getX(), mCamera.getCenterX() + (mCamera.getWidth()*playerFacing));
b.sprite.setVisible(true);
b.sprite.detachSelf();
scene.attachChild(b.sprite);
//Log.v("checkin", "works to here");
scene.bulletList.add(b);//<---------Crashes Here, works fine if this line is commented out
//Log.v("checkin", "still working?");
b.sprite.registerEntityModifier(mod);
}
Any insight would be helpful. Thanks
My guess is that you’ve got a NullPointerException, although it would help if you’d told us. This line:
requires that both
sceneandscene.bulletListare non-null. We can tell thatsceneis non-null, but I suspect thatscene.bulletListis null – where do you think you’re initializing it?(As an aside, I would strongly advise you to avoid public variables. I would probably put an
addBulletmethod intoGameScene– and probably make the type of the now-privatebulletListvariableList<PlayerBullet>… only specifyArrayList<E>when initializing the variable.)