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

Sebastian Saier
Sebastian Saier
20,045 Points

Unfortunately, Stormy has stopped...FATAL EXCEPTION: main java.lang.NullPointerException

Hi guys, I have run into a problem here and I don't seem to find the solution. When I click on the Hour button, the app crashes. In the log it says: 04-21 15:34:17.601 5174-5174/com.saier.sebastian.stormy E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException at com.saier.sebastian.stormy.adapters.HourAdapter$HourViewHolder.bindHour(HourAdapter.java:69) at com.saier.sebastian.stormy.adapters.HourAdapter.onBindViewHolder(HourAdapter.java:38) at com.saier.sebastian.stormy.adapters.HourAdapter.onBindViewHolder(HourAdapter.java:18) ......

Can anyone help me? Should I post my code to give more insight?

Joseph Bogaert
Joseph Bogaert
10,505 Points

Edit: Do you have all of your icons in your drawable folder? I.e. Rain, Sleet, Snow, Sunny?

If you do, can you post your HourAdapter.java class and Forecast.java Class?

6 Answers

David Burnham
David Burnham
7,023 Points

I had this same problem/error code and after checking many things, I found that the bug was not a null temp, but actually a mismatch to my label in the view. I had called it temperatureLabelView, but was referencing temperatureLabel in the code, so it could not find a home for the temperature.

I had the same issue, but figured the same David Turnham posted. That could be your problem.

Let us know if you solved it with this tip.

Yeah - agree; it could be a drawable issue.

But post your HourAdapter class and highlight with a comment, lines 18, 38 and 69. Posting the rest of your LogCat may assist a little too.

Something is pointing at nothing so we just need to figure out what. The error could be somewhere weird but those lines are highlighted initially.

Steve.

Sebastian Saier
Sebastian Saier
20,045 Points

Thanks for the answers!

I think i do have all of the icons in the drawable folder. When I click on daily, everything works fine. Here is my code:

HourAdapter Class

public class HourAdapter extends RecyclerView.Adapter<HourAdapter.HourViewHolder> {     //MARKED ERROR LINE

    private Hour[] mHours;
    private Context mContext;

    public HourAdapter(Context context, Hour[] hours) {
        mContext = context;
        mHours = hours;
    }

    @Override
    public HourViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
        View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.hourly_list_item, viewGroup, false);
        HourViewHolder viewHolder = new HourViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(HourViewHolder hourViewHolder, int i) { 
        hourViewHolder.bindHour(mHours[i]);                                         //MARKED ERROR LINE
    }

    @Override
    public int getItemCount() {
        return mHours.length;
    }

    public class HourViewHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener{ // nested inner class

        public TextView mTimeLabel;
        public TextView mSummaryLabel;
        public TextView mTemperatureLabel;
        public ImageView mIconImageView;

        public HourViewHolder(View itemView) { 

            super(itemView);

            mTimeLabel = (TextView) itemView.findViewById(R.id.timeLabel);
            mSummaryLabel = (TextView) itemView.findViewById(R.id.summaryLabel);
            mTemperatureLabel = (TextView) itemView.findViewById(R.id.temperatureLabel);
            mIconImageView = (ImageView) itemView.findViewById(R.id.iconImageView);

            itemView.setOnClickListener(this); 
        }

        public void bindHour(Hour hour) { 
            mTimeLabel.setText(hour.getHour());
            mSummaryLabel.setText(hour.getSummary());
            mTemperatureLabel.setText(hour.getTemperature() + "");              //MARKED ERROR LINE
            mIconImageView.setImageResource(hour.getIconId());
        }

        @Override
        public void onClick(View v) {
            String time = mTimeLabel.getText().toString();
            String temperature = mTemperatureLabel.getText().toString();
            String summary = mSummaryLabel.getText().toString();
            String message = String.format("At %s it will be %s and %s",
                    time,
                    temperature,
                    summary);
            Toast.makeText(mContext, message, Toast.LENGTH_LONG).show();

        }
    }
}

Forestcast Class

public class Forecast {
    private Current mCurrent;
    private Hour[] mHourlyForecast;
    private Day[] mDailyForecast;

    public Current getCurrent() {
        return mCurrent;
    }

    public void setCurrent(Current current) {
        mCurrent = current;
    }

    public Hour[] getHourlyForecast() {
        return mHourlyForecast;
    }

    public void setHourlyForecast(Hour[] hourlyForecast) {
        mHourlyForecast = hourlyForecast;
    }

    public Day[] getDailyForecast() {
        return mDailyForecast;
    }

    public void setDailyForecast(Day[] dailyForecast) {
        mDailyForecast = dailyForecast;
    }

    public static int getIconId(String iconString) {

        int iconId = R.mipmap.clear_day;

        if (iconString.equals("clear-day")) {
            iconId = R.mipmap.clear_day;
        }
        else if (iconString.equals("clear-night")) {
            iconId = R.mipmap.clear_night;
        }
        else if (iconString.equals("rain")) {
            iconId = R.mipmap.rain;
        }
        else if (iconString.equals("snow")) {
            iconId = R.mipmap.snow;
        }
        else if (iconString.equals("sleet")) {
            iconId = R.mipmap.sleet;
        }
        else if (iconString.equals("wind")) {
            iconId = R.mipmap.wind;
        }
        else if (iconString.equals("fog")) {
            iconId = R.mipmap.fog;
        }
        else if (iconString.equals("cloudy")) {
            iconId = R.mipmap.cloudy;
        }
        else if (iconString.equals("partly-cloudy-day")) {
            iconId = R.mipmap.partly_cloudy;
        }
        else if (iconString.equals("partly-cloudy-night")) {
            iconId = R.mipmap.cloudy_night;
        }

        return iconId;
    }
}

Logcat

04-22 18:17:26.702    1318-1318/com.saier.sebastian.stormy E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.saier.sebastian.stormy.adapters.HourAdapter$HourViewHolder.bindHour(HourAdapter.java:69)
            at com.saier.sebastian.stormy.adapters.HourAdapter.onBindViewHolder(HourAdapter.java:38)
            at com.saier.sebastian.stormy.adapters.HourAdapter.onBindViewHolder(HourAdapter.java:18)
            at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:4402)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3717)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3609)
            at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1859)
            at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1311)
            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1274)
            at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:525)
            at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2118)
            at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2415)
            at android.view.View.layout(View.java:14289)
            at android.view.ViewGroup.layout(ViewGroup.java:4562)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
            at android.view.View.layout(View.java:14289)
            at android.view.ViewGroup.layout(ViewGroup.java:4562)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:14289)
            at android.view.ViewGroup.layout(ViewGroup.java:4562)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
            at android.view.View.layout(View.java:14289)
            at android.view.ViewGroup.layout(ViewGroup.java:4562)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:14289)
            at android.view.ViewGroup.layout(ViewGroup.java:4562)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
            at android.view.View.layout(View.java:14289)
            at android.view.ViewGroup.layout(ViewGroup.java:4562)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:14289)
            at android.view.ViewGroup.layout(ViewGroup.java:4562)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
            at android.view.Choreographer.doCallbacks(Choreographer.java:562)
            at android.view.Choreographer.doFrame(Choreographer.java:532)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

So, getTemperature() is bringing back NULL. Hmmm ...

Can you put a breakpoint there and see what the state of play is when execution halts?

Steve.

Andrés Núñez Visbal
Andrés Núñez Visbal
3,652 Points

Please check that you implement the Parcelable right in the Hour class. Check the "writeToParcel" method and the constructor that receives Parcel as a parameter.