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

Mohamed Anser Ali Ameerali
Mohamed Anser Ali Ameerali
2,904 Points

why simple date formatter throwing null pointer exception?

I was working with Android List and Adatptor (Stormy app) and I was trying to convert Unix time to day and I ended up below,if I remove the try and catch block the code throwing error,In my log cat error shows as Null pointer exception (timezone),but to my surprise the code working correctly with formatter.setTimezone which I don't know how? (JAVA code) public String getDayOfTheWeek() {

    SimpleDateFormat formatter = new SimpleDateFormat("EEEE");
    try {
        formatter.setTimeZone(TimeZone.getTimeZone(getMtimezone()));

    }catch (Exception e)
    {
        Log.i("timezone", String.valueOf(e));

    }
    Date dateTime = new Date(mtime * 1000);
    String one = formatter.format(dateTime);
    Log.i("one",one);
    return one;    }

Output: 12-25 13:03:40.290 7280-7280/com.example.android.soonami I/timezone: java.lang.NullPointerException: id == null 12-25 13:03:40.290 7280-7280/com.example.android.soonami I/one: Sunday 12-25 13:03:40.291 7280-7280/com.example.android.soonami I/timezone: java.lang.NullPointerException: id == null 12-25 13:03:40.291 7280-7280/com.example.android.soonami I/one: Monday 12-25 13:03:40.292 7280-7280/com.example.android.soonami I/timezone: java.lang.NullPointerException: id == null 12-25 13:03:40.292 7280-7280/com.example.android.soonami I/one: Tuesday 12-25 13:03:40.293 7280-7280/com.example.android.soonami I/timezone: java.lang.NullPointerException: id == null 12-25 13:03:40.293 7280-7280/com.example.android.soonami I/one: Wednesday 12-25 13:03:40.294 7280-7280/com.example.android.soonami I/timezone: java.lang.NullPointerException: id == null 12-25 13:03:40.294 7280-7280/com.example.android.soonami I/one: Thursday 12-25 13:03:40.295 7280-7280/com.example.android.soonami I/timezone: java.lang.NullPointerException: id == null 12-25 13:03:40.295 7280-7280/com.example.android.soonami I/one: Friday 12-25 13:03:40.296 7280-7280/com.example.android.soonami I/timezone: java.lang.NullPointerException: id == null 12-25 13:03:40.296 7280-7280/com.example.android.soonami I/one: Saturday 12-25 13:03:40.298 7280-7280/com.example.android.soonami I/timezone: java.lang.NullPointerException: id == null 12-25 13:03:40.298 7280-7280/com.example.android.soonami I/one: Sunday