Georgi Koemdzhiev
Georgi Koemdzhiev

Reputation: 11931

Nullpointer Exception when using RecyclerView

I am following an android development tutorial (creating a weather app), but I am stuck at an error that I am getting I indlued the full logcat log at the end of the question. Does anyone has a clue why I am getting this error? I have checked everything more than 10 times. NODE: The version of the minSDK is 14 and that is why I am using the support library to use the Recycler view.

This is the layout xml file of the activity that uses the recycler view:

<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"
    android:background="@drawable/bg_gradient"
    tools:context="koemdzhiev.com.stormy.ui.HourlyForecastActivity">


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

This is the my code of the Recycler.Adapter:

public class HourAdapter extends RecyclerView.Adapter<HourAdapter.HourViewHolder> {
    private Hour[] mHours;

    public HourAdapter(Hour[] hours){
        mHours = hours;
    }


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

    @Override
    public void onBindViewHolder(HourViewHolder holder, int position) {
        holder.bindHour(mHours[position]);
    }

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

    public class HourViewHolder extends RecyclerView.ViewHolder{

        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);
        }

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

and this is the code in the activity:

public class HourlyForecastActivity extends Activity {

    private Hour[] mHours;
    //inject the RecyclerView as member variable
    @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);
        //if dealing with fixed size data, it is recommended to do the following...
        mRecyclerView.setHasFixedSize(true);
    }
}

And this is the logcat logs that I am getting:

05-22 22:56:00.394  32579-32579/koemdzhiev.com.stormy E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: koemdzhiev.com.stormy, PID: 32579
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
        at koemdzhiev.com.stormy.adapters.HourAdapter$HourViewHolder.bindHour(HourAdapter.java:61)
        at koemdzhiev.com.stormy.adapters.HourAdapter.onBindViewHolder(HourAdapter.java:33)
        at koemdzhiev.com.stormy.adapters.HourAdapter.onBindViewHolder(HourAdapter.java:16)
        at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:4805)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4106)

Upvotes: 1

Views: 2501

Answers (2)

GuilhE
GuilhE

Reputation: 11901

Your ImageView id is iconImaVeView but in your code you are calling iconImaGeView. That's strange because you should have a compile error unless you have an iconImageView somewhere else in other .xml file.

Upvotes: 3

Georgi Koemdzhiev
Georgi Koemdzhiev

Reputation: 11931

! There is a typo that I did't see! In the id of the IconImageView its iconImaveView with "v". I cannot believe that it was that simple.

Upvotes: 1

Related Questions