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 trialJavier Pacheco
3,963 PointsApp crashes when trying to refresh info
My app is crashing as soon as it gets launched and tries to retrieve the information. I know this because If I test it on airplane mode it doesn't crash and just show me the "Network unavailable" message. Here is the error I'm getting on the console: FATAL EXCEPTION: main Process: com.example.javierpacheco.stormy, PID: 8227 java.lang.NullPointerException: id == null at java.util.TimeZone.getTimeZone(TimeZone.java:349) at com.example.javierpacheco.stormy.CurrentWeather.getFormattedTime(CurrentWeather.java:88) at com.example.javierpacheco.stormy.MainActivity.updateDisplay(MainActivity.java:144) at com.example.javierpacheco.stormy.MainActivity.access$500(MainActivity.java:33) at com.example.javierpacheco.stormy.MainActivity$2$3.run(MainActivity.java:111) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
And here is the line of code it is referring: formatter.setTimeZone(TimeZone.getTimeZone(getTimeZone()));
1 Answer
Gavin Ralston
28,770 Pointsjava.lang.NullPointerException:
id == null at java.util.TimeZone.getTimeZone(TimeZone.java:349) at
com.example.javierpacheco.stormy.CurrentWeather.getFormattedTime(CurrentWeather.java:88) at
com.example.javierpacheco.stormy.MainActivity.updateDisplay(MainActivity.java:144) at...
If your CurrentWeather object populated just fine with the json data, be sure to make sure in your main activity, in the getCurrentDetails method, you're actually returning the CurrentWeather object referred to by currentWeather and not the placeholder from an earlier tutorial, where it just quickly returned a brand new, unpopulated CurrentWeather object.
return currentWeather; // this is what you want
// but this may be near the last line of the method
CurrentWeather currentWeather = new CurrentWeather();
// which means a brand new, empty, and "null-pointery" object will actually get returned
return currentWeather;
That's just a guess, but if you followed along that far without trouble, chances are that "create a new CurrentWeather object right after you populated the first one" bit is sticking around in your code
Based on the stack trace, you're calling updateDisplay, trying to get the formatted time from the CurrentWeather object (which if you just created a new one to replace the one you just built up, will have nothing to return), and then the getTimeZone is complaining because it was passed nothing, and one of its method signatures will throw "null" back if it can't figure out the time zone it's working with.
Yael P.
2,816 PointsYael P.
2,816 PointsCould you please copy and paste your MainActivity here?