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 Android Lists and Adapters (2015) Lists with RecyclerViews Using a Layout Manager

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

NullPointerException on Hourly Button Click

I wish I could work out what is wrong with the hour button on click but I can't make why way through this stack trace.

Anybody who speaks logcat might be able to help

Latest Github Repo Error now fixed :).... https://github.com/jg-digital-media/Stormy-Android-Lists-and-Adapters-

06-14 10:48:12.959 3534-3534/teamtreehouse.com.stormy E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: teamtreehouse.com.stormy, PID: 3534
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{teamtreehouse.com.stormy/teamtreehouse.com.stormy.ui.DailyForecastActivity}: java.lang.NullPointerException: Attempt to get length of null array
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                         Caused by: java.lang.NullPointerException: Attempt to get length of null array
                                                                            at teamtreehouse.com.stormy.ui.DailyForecastActivity.onCreate(DailyForecastActivity.java:27)
                                                                            at android.app.Activity.performCreate(Activity.java:6237)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:148) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
06-14 10:53:12.972 3534-3534/? I/Process: Sending signal. PID: 3534 SIG: 9

1 Answer

Ben Deitch
STAFF
Ben Deitch
Treehouse Teacher

Hey Jonathan!

You're just passing in the wrong Activity for your hour button :)

@OnClick (R.id.hourlyButton)
    public void startHourlyForecast(View view) {
        Intent intent2 = new Intent(this, DailyForecastActivity.class); <--------
        intent2.putExtra(HOURLY_FORECAST, forecast.getHourlyForecast());
        startActivity(intent2);
    }

Though once you fix that you'll run into another error in HourAdapter. In the constructor you're passing in an 'hours' variable, but then you set that 'hours' variable equal to itself. Instead you want to set the 'hours' field equal to your new 'hours' parameter. You can specify the 'hours' field by using the 'this' keyword:

public HourAdapter(Hour[] hours) {
        this.hours = hours;
    }
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Thanks Ben, you have saved my life once again :)

Regarding the first point.... how did I not see that myself before? :) I did copy and paste that portion of the code from the first intent but I'm disappointed I didn't catch that.

Thanks again