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 Build a Weather App (2015) Hooking Up the Model to the View Plugging in the Data

Simon Andersson
Simon Andersson
6,880 Points

NullPointerException on updateDisplay()

I'm getting this error "java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference" when trying to run my code. Now I'm not sure if this is how you properly post code here, but anyway this is what it looks like.

I have this following line in my onCreate() method ButterKnife.bind(this);

I also have the method updateDisplay itself

private void updateDisplay() { mTemperatureLabel.setText(mMCurrentWeather.getTemperature() + ""); }

And finally, the runOnUiThread method

mMCurrentWeather = getCurrentDetails(jsonData); runOnUiThread(new Runnable() { @Override public void run() { updateDisplay(); } });

I've read through other posts but cant seem to fix it. Also, in my build.gradle (module) I have this under depedencies

compile 'com.jakewharton:butterknife:8.0.1'

I don't have apt 'com.jakewharton:butterknife-compiler:8.0.1' tho, because when I paste it, it gives me an error saying

Gradle DSL method not found: 'apt()' Possible causes:

The project 'Stormy' may be using a version of Gradle that does not contain the method. Gradle Settings

The build file may be missing a Gradle plugin. Apply Gradle Plugin

Please help :(

3 Answers

Seth Kroger
Seth Kroger
56,413 Points

See: https://teamtreehouse.com/community/more-deprecated-butterknife-code

Ben Jakuben Ben Deitch This ButterKnife 8 update is causing a few issues. We need some updated teacher's notes on this.

Simon Andersson
Simon Andersson
6,880 Points

I got it working now, thanks a lot! :)

Olalekan Bisiriyu
Olalekan Bisiriyu
562 Points

Please simon how did you fix it. thanks

Simon Andersson
Simon Andersson
6,880 Points

I don't remember exactly, but take a look at the link Seth posted and you should be good :)

If you are receiving an error that indicates you are trying to call setText() on a null object, make sure you bind View objects with the layout views by calling ButterKnife.bind(this) as follows:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

    ...
Simon Andersson
Simon Andersson
6,880 Points

Okay thanks, I'll keep that in mind :)

Simon Andersson I know your problem was different. This was an answer for those who might have this problem. :)