Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,274 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!!!