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 trialQian Wu
1,169 Pointsjava.lang.NullPointerException (Look around MainActivity.java line 19)
When I tried to set the current scene there was a null-pointer exception
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
19,940 PointsHey @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
19,940 Pointspublic 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
19,940 PointsThe 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.
Mark Szente
Courses Plus Student 21,053 PointsHi!
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();
}
} '''
Mark Szente
Courses Plus Student 21,053 Pointsoh, indeed! Thanks a lot Kirill for your quick help!
Darnell Cephus
4,208 PointsWhy 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
19,940 Pointshey 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'
Darnell Cephus
4,208 PointsI see that logically now lol, thanks!
Kirill Babkin
19,940 Pointssure man, any time