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 Making Hour Parcelable

Noah Schill
Noah Schill
10,020 Points

NullPointerException

Here is the error I get when trying to run my code and when trying to run the project code. I'm guessing I have something wrong with my support library or with some gradle config.

-06 12:46:36.972 32023-32023/teamtreehouse.com.stormy E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: teamtreehouse.com.stormy, PID: 32023 java.lang.NullPointerException at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764) at android.view.View.measure(View.java:16612) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455) at android.view.View.measure(View.java:16612) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5147) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:16612) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5147) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) at android.view.View.measure(View.java:16612) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5147) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:16612) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5147) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) at android.view.View.measure(View.java:16612) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5147) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291) at android.view.View.measure(View.java:16612) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2065) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1192) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1401) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1079) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5948) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) at android.view.Choreographer.doCallbacks(Choreographer.java:574) at android.view.Choreographer.doFrame(Choreographer.java:544) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5102) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method)

Noah Schill
Noah Schill
10,020 Points

Ben Jakuben , Do you have any suggestions? I cannot continue with the course beyond this point because of this error.

3 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

This error log indicates this might be a problem in the layout file you are using for the RecyclerView item. Can you post the XML for your item layout?

Noah Schill
Noah Schill
10,020 Points

This happens on both the Course code and with my own code. Here is the course code that I am trying to use:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                tools:context=".MainActivity"
                android:background="@drawable/bg_gradient">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="--"
        android:id="@+id/temperatureLabel"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:textColor="@android:color/white"
        android:textSize="150sp"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/degreeImageView"
        android:layout_alignTop="@+id/temperatureLabel"
        android:layout_toRightOf="@+id/temperatureLabel"
        android:layout_toEndOf="@+id/temperatureLabel"
        android:src="@drawable/degree"
        android:layout_marginTop="50dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="..."
        android:id="@+id/timeLabel"
        android:layout_above="@+id/degreeImageView"
        android:layout_centerHorizontal="true"
        android:textColor="#80ffffff"
        android:textSize="18sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Alcatraz Island, CA"
        android:id="@+id/locationLabel"
        android:layout_above="@+id/timeLabel"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="60dp"
        android:textColor="@android:color/white"
        android:textSize="24sp"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iconImageView"
        android:layout_alignBottom="@+id/locationLabel"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:src="@drawable/cloudy_night"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/temperatureLabel"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:weightSum="100"
        android:id="@+id/linearLayout">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="50">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="HUMIDITY"
                android:id="@+id/humidityLabel"
                android:textColor="#80ffffff"
                android:gravity="center_horizontal"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="--"
                android:id="@+id/humidityValue"
                android:textColor="@android:color/white"
                android:textSize="24sp"
                android:gravity="center_horizontal"/>
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="50">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="RAIN/SNOW?"
                android:id="@+id/precipLabel"
                android:textColor="#80ffffff"
                android:gravity="center_horizontal"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="--"
                android:id="@+id/precipValue"
                android:textColor="@android:color/white"
                android:textSize="24sp"
                android:gravity="center_horizontal"/>
        </LinearLayout>
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Getting current weather..."
        android:id="@+id/summaryLabel"
        android:layout_below="@+id/linearLayout"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:textColor="@android:color/white"
        android:textSize="18dp"
        android:gravity="center_horizontal"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/refreshImageView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/refresh"/>

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_alignBottom="@+id/refreshImageView"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="HOURLY"
            android:id="@+id/hourlyButton"
            android:layout_weight="1"
            android:background="#40ffffff"
            android:textColor="#ffffffff"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="7 DAY"
            android:id="@+id/dailyButton"
            android:layout_weight="1"
            android:background="#40ffffff"
            android:textColor="#ffffffff"
            android:layout_marginLeft="2dp"/>
    </LinearLayout>

</RelativeLayout>

Assuming all is well with the downloadable file, I'm assuming that there is something wrong with my support library or perhaps the way it is being referenced. I tried uninstalling and re-installing the support library using the SDK manager, but still no dice.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

That's your activity layout file, which looks good. Do you have one called something like hourly_list_item.xml? The error message is talking about measuring items in the RecylerView, which indicates the problem may be in that layout, which is used when the RecyclerView is trying to display items.

Noah Schill
Noah Schill
10,020 Points

Ben Jakuben , Sorry! Silly mistake to post my MainActivity. Here is my item layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="8dp"
                android:paddingBottom="8dp"
                tools:background="@drawable/bg_gradient">

    <TextView
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:id="@+id/timeLabel"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textColor="#ffffffff"
        android:textSize="24sp"
        tools:text="12 PM"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iconImageView"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/timeLabel"
        android:layout_toEndOf="@+id/timeLabel"
        android:src="@drawable/partly_cloudy"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="100"
        android:id="@+id/temperatureLabel"
        android:layout_alignParentTop="false"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:textColor="#ffffffff"
        android:textSize="24sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/summaryLabel"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/iconImageView"
        android:layout_toEndOf="@+id/iconImageView"
        android:textColor="#ffffffff"
        android:layout_toLeftOf="@+id/temperatureLabel"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        tools:text="Partly cloudy"/>
</RelativeLayout>

For good measure, here is my activity_hourly_forecast.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context="teamtreehouse.com.stormy.ui.HourlyForecastActivity"
                android:background="@drawable/bg_gradient">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/reyclerView" />

</RelativeLayout>

Again, these are the exact project files downloaded from the lesson. It must be something on my end with the configurations.

Noah Schill
Noah Schill
10,020 Points

Also spotted a small typo in the downloadable project code. android:id="@+id/reyclerView" should be android:id="@+id/recyclerView". Still didn't fix the problem tho :/

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Okay, yes, as you said, everything matches up! Hmmm. Can you check your HourlyForecastActivity code to see if you are setting a layout manager, like this? http://stackoverflow.com/a/28492351/475217

Noah Schill
Noah Schill
10,020 Points

Yes, I do have a layout manager set:

package com.nrsmac.stormy.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.nrsmac.stormy.R;
import com.nrsmac.stormy.adapters.HourAdapter;
import com.nrsmac.stormy.weather.Hour;

import java.util.Arrays;

import butterknife.ButterKnife;
import butterknife.InjectView;


public class HourlyForecastActivity extends ActionBarActivity {

    private Hour[] mHours;

    @InjectView(R.id.recyclerView) RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hourly_forecast);
        ButterKnife.inject(this);

        Intent intent = getIntent();
        Parcelable[] parcelables = intent.getParcelableArrayExtra(MainActivity.HOURLY_FORECAST);
        mHours = Arrays.copyOf(parcelables, parcelables.length, Hour[].class);

        HourAdapter adapter = new HourAdapter(mHours);
        mRecyclerView.setAdapter(adapter);

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);

        mRecyclerView.setHasFixedSize(true);

    }
}
Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hmmm...I'm at quite a loss. Can you zip up your project and either paste a link here or email it to support@teamtreehouse.com and ask them to forward it to me?

Noah Schill
Noah Schill
10,020 Points

Ben Jakuben , I use the downloadable code from the project, and recieve the same error. I'll send you my code anyways if you want it. It's on my GitHub repo at github.com/nrsmac/Stormy. My commits are very confusing but the code I am using and that is most recent is on master.

Noah Schill
Noah Schill
10,020 Points

Ben Jakuben , As if by magic, the problem resolved itself when adding the toast to view the weather conditions and temperature. I'm not sure if it was because I had rebooted, but now everything is working well. Thank you for your help.

donni gunawan
donni gunawan
1,853 Points

Array length are setup, but it says return null array. Here is the error: ''' 06-19 11:15:52.725 20751-20751/donnigunawan.com.weatherapp E/AndroidRuntime: FATAL EXCEPTION: main Process: donnigunawan.com.weatherapp, PID: 20751 java.lang.RuntimeException: Unable to start activity ComponentInfo{donnigunawan.com.weatherapp/donnigunawan.com.weatherapp.ui.HourlyForecastActivity}: java.lang.NullPointerException: Attempt to get length of null array at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) 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:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.NullPointerException: Attempt to get length of null array at donnigunawan.com.weatherapp.ui.HourlyForecastActivity.onCreate(HourlyForecastActivity.java:32) at android.app.Activity.performCreate(Activity.java:5933) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)  at android.app.ActivityThread.access$800(ActivityThread.java:144)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5221)  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:899)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)  '''