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

Mark Junge
Mark Junge
1,374 Points

Exception

What should i look for when i get an exception like this?

Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference.

I think there might be something wrong with this part from one of my activities, because it is some of the newest i have build in the activity.

public QuestionsActivity(){ mFirstTextView = (TextView) findViewById(R.id.questionText); mFirstTextView.setText("Am I a question?");

The meaning is that when the activity get started then some text should appear in a textview.

I hope someone understand my question.

Thank you for your time

thoughts from here

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

"Attempt to invoke virtual method ... on a null object reference." happens because something wasn't initialized properly and was left as null. In Android, you'll often need system resources, such as context and the layout engine to initialize things properly. This is done later than the constructor call, so it's impossible to get a meaningful value to initialize a view with.

That's why as a matter of practice you should never use a constructor to setup an activity, but always use the lifecycle methods like onCreate() instead.

Mark Junge
Mark Junge
1,374 Points

Hey Seth Thank you for your answer, it worked as you probably expected :-)

I hope i can get more knowledge from you next time i have a problem that i cant find solution on:-)

Kind regards Mark