Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Animations and Transitions The Transitions Framework Transitioning Back and Forth

Qian Wu
Qian Wu
1,169 Points

java.lang.NullPointerException (Look around MainActivity.java line 19)

When I tried to set the current scene there was a null-pointer exception

MainActivity.java
public class MainActivity extends Activity {

    protected TransitionManager mTransitionManager;
    protected Scene mScene1;
    protected Scene mScene2;
    protected Scene mCurrentScene;
    protected TransitionSet mForwardSet;
    protected TransitionSet mBackwardSet;
    //protected TransitionManager mTransitionManager;
    // Some code omitted for brevity

    public void onButtonClicked(View view) {
        if (mCurrentScene == mScene2) {
            mCurrentScene = mScene1;
        }
        else {
            mCurrentScene = mScene2;
        }
        mTransitionManager.transitionTo(mCurrentScene);
    }

    protected void setupTransitions() {
        // These methods are setting up scenes and transitions sets like in the video
        initScenes();
        initSets(); 
        TransitionManager mTransitionManager = new TransitionManager();
        // Start here!
        mTransitionManager.setTransition(mScene1,mScene2,mForwardSet);
        mTransitionManager.setTransition(mScene2,mScene1,mBackwardSet);
      mScene1.enter();

    }
}

6 Answers

Kirill Babkin
Kirill Babkin
19,940 Points

Hey @Qian Wu I think you have the this object commented out on line 9

//protected TransitionManager mTransitionManager;

Then on the line 19 you referring to that object and trying to call a method on mTransitionManager. When it was actually never created in memory, that is why it is giving you NullPointerException.

try to uncomment line 9 and see if it helps.

Thank you.

Kirill Babkin
Kirill Babkin
19,940 Points
public class MainActivity extends Activity {

    protected TransitionManager mTransitionManager;
    protected Scene mScene1;
    protected Scene mScene2;
    protected Scene mCurrentScene;
    protected TransitionSet mForwardSet;
    protected TransitionSet mBackwardSet;

    // Some code omitted for brevity

    public void onButtonClicked(View view) {
        if (mCurrentScene == mScene2) {
            mCurrentScene = mScene1;
        }
        else {
            mCurrentScene = mScene2;
        }
        mTransitionManager.transitionTo(mCurrentScene);
    }

    protected void setupTransitions() {
        // These methods are setting up scenes and transitions sets like in the video
        initScenes();
        initSets(); 

        // Start here!
        mTransitionManager = new TransitionManager();
        mTransitionManager.setTransition(mScene1,mScene2,mForwardSet);
        mTransitionManager.setTransition(mScene2,mScene1,mBackwardSet);
        mScene1.enter();
    }
}

This works for the challenge..

Kirill Babkin
Kirill Babkin
19,940 Points

The only error you made was this line of code in setupTransitions()

TransitionManager mTransitionManager = new TransitionManager();

when you create an object inside method then this object belongs to a local scope. thus you can not reference it from different method.

Here you already declared an object of type TransitionManager it is now in global scope - every method in your MainActivity can access it but you did not assign any value to it yet. // by default it has value of null

    protected TransitionManager mTransitionManager;
    protected Scene mScene1;
    protected Scene mScene2;
    protected Scene mCurrentScene;
    protected TransitionSet mForwardSet;
    protected TransitionSet mBackwardSet;

after in setupTransitions() you make this variable mTransitionManager reference a newly created object.

  mTransitionManager = new TransitionManager();

i hope it helped. let me know if you have any questions.

Hi!

i still have this error, and can't get around it. I have observed the code above and mine is the same, without line 9. Its funny because mTransationManager was declared first in the code, before it was copied and commented out...

please help me to get through this! thanks in advance:

''' public class MainActivity extends Activity {

protected TransitionManager mTransitionManager;
protected Scene mScene1;
protected Scene mScene2;
protected Scene mCurrentScene;
protected TransitionSet mForwardSet;
protected TransitionSet mBackwardSet;

// Some code omitted for brevity

public void onButtonClicked(View view) {
    if (mCurrentScene == mScene2) {
        mCurrentScene = mScene1;
    }
    else {
        mCurrentScene = mScene2;
    }
    mTransitionManager.transitionTo(mCurrentScene);
 }

protected void setupTransitions() {
    // These methods are setting up scenes and transitions sets like in the video
    initScenes();
    initSets(); 

    // Start here!
    TransitionManager mTransitionManager = new TransitionManager();
    mTransitionManager.setTransition(mScene1, mScene2, mForwardSet);
    mTransitionManager.setTransition(mScene2, mScene1, mBackwardSet);
    mScene1.enter();
}

} '''

oh, indeed! Thanks a lot Kirill for your quick help!

Why does mTransitionManager = new TransitionManager(); work after taking away the TransitionManager behind the mTransitionManager variable??? It shouldn't matter right? or is it just the simulated coder on here were using making a big deal out of it?

Kirill Babkin
Kirill Babkin
19,940 Points

hey Darnell Cephus, it does matter, variables in java reference objects, every time you declare a new var. you create a spot in memory that is going to hold a reference to an object and not object itself.

furthermore variables created inside methods belong to a local scope. you can access them only inside this method. that is why when you do TransitionManager mTransitionManager = new TransitionManager(); inside setupTransitions(); method, this mTransitionManager variable can not be accessed from onButtonClicked method. -- essentially you create a new Variable of type TransitionManager mTransitionManager in local scope.

When we you do mTransitionManager = new TransitionManager(); you perform an assignment of a new value to a variable mTransitionManager that was declared in global scope thus accessible by all members of this object 'MainActivity'

I see that logically now lol, thanks!