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 trialSimon Andersson
6,880 PointsNullPointerException 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
56,413 PointsSee: 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
6,880 PointsI got it working now, thanks a lot! :)
Olalekan Bisiriyu
562 PointsPlease simon how did you fix it. thanks
Simon Andersson
6,880 PointsI don't remember exactly, but take a look at the link Seth posted and you should be good :)
V F
Courses Plus Student 121 PointsIf 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
6,880 PointsOkay thanks, I'll keep that in mind :)
V F
Courses Plus Student 121 PointsSimon Andersson I know your problem was different. This was an answer for those who might have this problem. :)