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 Working with JSON Cleaning Up the Date and Time

KARTIK BHASIN
KARTIK BHASIN
9,299 Points

Getting error in this method

//My function public String getFormattedTime() { SimpleDateFormat formatter = new SimpleDateFormat("h:mm a"); formatter.setTimeZone(TimeZone.getTimeZone(timeZone)); Date dateTime = new Date(time*1000); return formatter.format(dateTime); }

//From where it is being called private CurrentWeather getCurrentDetails(String jsonData) throws JSONException{ JSONObject forecast = new JSONObject(jsonData); String timezone = forecast.getString("timezone"); Log.i(TAG,"From JSON: " + timezone); JSONObject currently = forecast.getJSONObject("currently"); CurrentWeather currentWeather = new CurrentWeather(); currentWeather.setHumidity(currently.getDouble("humidity")); currentWeather.setTime(currently.getLong("time")); currentWeather.setIcon(currently.getString("icon")); currentWeather.setLocationLabel("Alcatraz Island, CA"); currentWeather.setPrecipChance(currently.getDouble("precipProbability")); currentWeather.setSummary(currently.getString("summary")); currentWeather.setTemperature(currently.getDouble("temperature")); currentWeather.setTimeZone(timezone); Log.d(TAG,currentWeather.getFormattedTime()); return currentWeather; } I recieve null pointer exception in line Log.d(TAG,currentWeather.getFormattedTime()) under logcat - verbose section.

KARTIK BHASIN
KARTIK BHASIN
9,299 Points

07-27 21:53:33.767 15704-15722/com.example.nikhil.stormy E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher Process: com.example.nikhil.stormy, PID: 15704 java.lang.NullPointerException: id == null at java.util.TimeZone.getTimeZone(TimeZone.java:561) at com.example.nikhil.stormy.CurrentWeather.getFormattedTime(CurrentWeather.java:49) at com.example.nikhil.stormy.MainActivity.getCurrentDetails(MainActivity.java:80) at com.example.nikhil.stormy.MainActivity.access$100(MainActivity.java:23) at com.example.nikhil.stormy.MainActivity$1.onResponse(MainActivity.java:51) at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153) at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764) 07-27 21:53:33.796 15704-15722/com.example.nikhil.stormy I/Process: Sending signal. PID: 15704 SIG: 9

1 Answer

Ari Misha
Ari Misha
19,323 Points

Hiya there! You're getting a NullPointerException coz getTimeZone() method expects an ID which can be of type String. Here is the snippet i picked out from the Java docs on TimeZone class:

public static TimeZone getTimeZone(String ID)
Gets the TimeZone for the given ID.
Parameters:
ID - the ID for a TimeZone, either an abbreviation such as "PST", a full name such as "America/Los_Angeles", or a custom ID such as "GMT-8:00". Note that the support of abbreviations is for JDK 1.1.x compatibility only and full names should be used.
Returns:
the specified TimeZone, or the GMT zone if the given ID cannot be understood.

I hope it helped!

~ Ari

KARTIK BHASIN
KARTIK BHASIN
9,299 Points

My Inbuilt getter function was wrong!!!