Reputation: 174
I am newbie to android game development. I've been trying an example of game development but some how unable to understand the cause of null point exception in android.
public void onLoadResources() {
this.mBitmapTextureAtlas = new BitmapTextureAtlas(512, 512,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(this.mBitmapTextureAtlas, this, "Player.png",
0, 0);
this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
final int PlayerX = (int)(mCamera.getWidth() / 2);
final int PlayerY = (int) ((mCamera.getHeight() - mPlayerTextureRegion
.getHeight()) / 2);
this.player = new Sprite(PlayerX, PlayerY, this.mPlayerTextureRegion);
this.player.setScale(2);
this.mMainScene.attachChild(this.player);
}
The last line "this.mMainScene.attachChild(this.player);"
is causing null point exception, even though everything is intialized. After commenting this line everything works fine but display is without sprite.
here is the code where mMainScene is intialized
public Scene onLoadScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
this.mMainScene = new Scene();
this.mMainScene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));
return mMainScene;
}
Upvotes: 2
Views: 591
Reputation: 3044
You have not initialized mMainScene. You cannot reference (i.e. attach Children / call methods until you've defined what mMainScene is.)
Could you check if mMainScene is null, or move your assignment of mMainScene into onLoadResources()?
Can you move the mMainScene definition from onLoadScene into onLoadResources?
public void onLoadResources() {
this.mBitmapTextureAtlas = new BitmapTextureAtlas(512, 512,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(this.mBitmapTextureAtlas, this, "Player.png",
0, 0);
this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
final int PlayerX = (int)(mCamera.getWidth() / 2);
final int PlayerY = (int) ((mCamera.getHeight() - mPlayerTextureRegion
.getHeight()) / 2);
this.player = new Sprite(PlayerX, PlayerY, this.mPlayerTextureRegion);
this.player.setScale(2);
this.mMainScene = new Scene();
this.mMainScene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));
this.mMainScene.attachChild(this.player);
}
public Scene onLoadScene() {
return mMainScene;
}
Edit:
Based on an example of Snake it appears that you should add children to the scene in the onLoadScene method:
public Scene onLoadScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
this.mScene = new Scene();
for(int i = 0; i < LAYER_COUNT; i++) {
this.mScene.attachChild(new Entity());
}
Upvotes: 4