Question

I am trying to add particle effect in my game. No error but showing logcat when running. Below is my code and logcat.

@Override
public void onLoadResources() {

    //prepare a container for particle
    mParticleBitmapTextureAtlas = new BitmapTextureAtlas(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);

    // setting assets path for easy access
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

    mParticleTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mParticleBitmapTextureAtlas, this, "particle_fire.png", 0,0);

    // loading textures in the engine
    mEngine.getTextureManager().loadTexture(this.mParticleBitmapTextureAtlas);
 }

@Override
public Scene onLoadScene() {
    mEngine.registerUpdateHandler(new FPSLogger());
            mMainScene = new Scene();
    mMainScene.setBackground(new ColorBackground(0f, 0f, 0f));

    //particle system 

    /* Left to right Particle System. */
    {
        final ParticleSystem particleSystem = new ParticleSystem(new PointParticleEmitter(0, CAMERA_HEIGHT), 6, 10, 70, this.mParticleTextureRegion);
        particleSystem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE);

        particleSystem.addParticleInitializer(new VelocityInitializer(15, 22, -60, -90));
        particleSystem.addParticleInitializer(new AccelerationInitializer(5, 15));
        particleSystem.addParticleInitializer(new RotationInitializer(0.0f, 360.0f));
        particleSystem.addParticleInitializer(new ColorInitializer(1.0f, 0.0f, 0.0f));

        particleSystem.addParticleModifier(new ScaleModifier(0.5f, 2.0f, 0, 5));
        particleSystem.addParticleModifier(new ExpireModifier(11.5f));
        particleSystem.addParticleModifier(new AlphaModifier(1.0f, 0.0f, 2.5f, 3.5f));
        particleSystem.addParticleModifier(new AlphaModifier(0.0f, 1.0f, 3.5f, 4.5f));
        particleSystem.addParticleModifier(new ColorModifier(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 11.5f));
        particleSystem.addParticleModifier(new AlphaModifier(1.0f, 0.0f, 4.5f, 11.5f));

        this.mMainScene.getLastChild().attachChild(particleSystem);
    }
          return mMainScene;
         }

////////////////////logcat///////////////////////

            06-11 18:51:59.588: E/AndroidRuntime(1539): FATAL EXCEPTION: main
06-11 18:51:59.588: E/AndroidRuntime(1539): java.lang.NullPointerException
06-11 18:51:59.588: E/AndroidRuntime(1539):     at com.example.AndEngineTest.AndEngineTest.onLoadScene(AndEngineTest.java:251)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at org.anddev.andengine.ui.activity.BaseGameActivity.doResume(BaseGameActivity.java:169)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at org.anddev.andengine.ui.activity.BaseGameActivity.onWindowFocusChanged(BaseGameActivity.java:85)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:2366)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at android.view.View.dispatchWindowFocusChanged(View.java:5735)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:851)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2557)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at android.os.Looper.loop(Looper.java:137)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at android.app.ActivityThread.main(ActivityThread.java:4424)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at java.lang.reflect.Method.invokeNative(Native Method)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at java.lang.reflect.Method.invoke(Method.java:511)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at dalvik.system.NativeStart.main(Native Method)
Was it helpful?

Solution

The NullPointerException is occurring on line 251 which you've said is this:

this.mMainScene.getLastChild().attachChild(particleSystem);

You've previously accessed mMainScene without a problem, so it looks as if getLastChild() is returning null.

OTHER TIPS

getLastChild() returns null when given Entity (in this case Scene) has no children. See Entity source code.

Your Scene contains no children and you want to attach particle effect to the last child, hence the NullPointerException. Instead of attaching the particle system to last child, attach it to mMainScene directly:

this.mMainScene.attachChild(particleSystem);

Try this.mMainScene.attachChild(particleSystem); without the .getLastChild().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top