简体   繁体   中英

Error in RecyclerViewAdapter's onBindViewHolder() Method

Guys it could seems similar to you like other NullPionterException Questions and before mark it as duplicate please read it and try to understand my problem first and point it out where i am wrong.

Actually my app is accessing some JOSN data then parse it and make a POJO object and put that object in some ArrayList<SchemesHitsPojoModel> schemesListHitsWise then i send this ArrayList to my RecyclerViewAdapter Class and then displaying it in RecyclerView.

But inside my onBindViewHolder method when i try to set some text on schemeName then it gives me following error. Help me guys i am trying to resolve this problem form last couple of days and read lots of answers but still this error remains.

LogCat

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
            at com.dovezeal.gapp.adapters.HitsOnSchemesDataDisplayAdapter.onBindViewHolder(HitsOnSchemesDataDisplayAdapter.java:71)
            at com.dovezeal.gapp.adapters.HitsOnSchemesDataDisplayAdapter.onBindViewHolder(HitsOnSchemesDataDisplayAdapter.java:21)
            at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5277)
            at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5310)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4568)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4461)
            at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1962)
            at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1371)
            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1334)
            at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:563)
            at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2847)
            at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3145)
            at android.view.View.layout(View.java:15671)
            at android.view.ViewGroup.layout(ViewGroup.java:5038)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
            at android.view.View.layout(View.java:15671)
            at android.view.ViewGroup.layout(ViewGroup.java:5038)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
            at android.view.View.layout(View.java:15671)
            at android.view.ViewGroup.layout(ViewGroup.java:5038)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
            at android.view.View.layout(View.java:15671)
            at android.view.ViewGroup.layout(ViewGroup.java:5038)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
            at android.view.View.layout(View.java:15671)
            at android.view.ViewGroup.layout(ViewGroup.java:5038)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
            at android.view.View.layout(View.java:15671)
            at android.view.ViewGroup.layout(ViewGroup.java:5038)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
            at android.view.View.layout(View.java:15671)
            at android.view.ViewGroup.layout(ViewGroup.java:5038)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            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:903)
            at c

MyAdapter File

 @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        MyViewHolder viewHolder;
        switch (success){
            case(0):
                view = layoutInflater.inflate(R.layout.custom_no_record_found_msg_screen, parent, false);
                break;
            case(1):
                view = layoutInflater.inflate(R.layout.custom_recyclerview_row_each_scheme_hits, parent,false);

        }
        viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

@Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        SchemesHitsPojoModel firstObject = schemesListHitsWise.get(0);
        success = firstObject.getSuccess();
        if(success == 0 ){
            Log.d("SORRY!, "," We don't have any records to show you.");
        }
        else{
            SchemesHitsPojoModel currentSchemeRow =  schemesListHitsWise.get(position);
            holder.schemeName.setText(currentSchemeRow.getSchemeName());
            int langId = currentSchemeRow.getLanguage_id();
            switch(langId){
                case(1):
                    holder.languageName.setText("Punjabi");
                    break;
                case(2):
                    holder.languageName.setText("English");
            }

            holder.schemeViewDate.setText(currentSchemeRow.getSchemeViewDate());
            holder.schemeHits.setText(String.valueOf(currentSchemeRow.getSchemeHits()));
            holder.viewIcon.setImageResource(R.drawable.ic_remove_red_eye_black_18dp);
            //holder.totalHitsOnThisScheme.setText(currentSchemeRow.getSchemeTotalHitsCount());
        }
    }

// MyViewHolder
    public class MyViewHolder extends RecyclerView.ViewHolder{
        TextView schemeName, schemeViewDate, languageName, schemeHits,totalHitsOnThisScheme;
        ImageView viewIcon;

        public MyViewHolder(View itemView) {
            super(itemView);
            schemeName = (TextView) itemView.findViewById(R.id.hitsOnSchemeName);
            languageName = (TextView) itemView.findViewById(R.id.hitsOnLangName);
            viewIcon = (ImageView) itemView.findViewById(R.id.hitsOnViewIcon);
            schemeViewDate = (TextView) itemView.findViewById(R.id.hitsOnSchemeViewDate);
            schemeHits = (TextView) itemView.findViewById(R.id.hitsOnSchemeHits);
            //totalHitsOnThisScheme = (TextView) itemView.findViewById(R.id.totalHitsOnThisScheme);
        }
    }

XML File of RecyclerView's Custom Row

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">


    <GridLayout
        android:id="@+id/gridLayout4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp">

        <TextView
            android:id="@+id/hitsOnSchemeName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_row="0"
            android:width="270dp"
            android:text="Name of the Scheme"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#000"
            android:textSize="14dp" />

        <ImageView
            android:id="@+id/hitsOnViewIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_row="0"
            android:src="@drawable/ic_remove_red_eye_black_18dp" />

        <TextView
            android:id="@+id/hitsOnSchemeHits"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:layout_gravity="left|center"
            android:layout_marginLeft="3dp"
            android:layout_row="0"
            android:text="5"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="12dp" />

        <TextView
            android:id="@+id/hitsOnLangName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_row="1"
            android:width="270dp"
            android:text="Language of scheme"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="10dp" />

        <TextView
            android:id="@+id/hitsOnSchemeViewDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_columnSpan="2"
            android:layout_row="1"
            android:text="1989-12-09"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="12dp" />
    </GridLayout>

</RelativeLayout>

i think you problem is here

holder.schemeName.setText(currentSchemeRow.getSchemeName());

change this to

holder.schemeName.setText(currentSchemeRow.get(position).getSchemeName() );

move

SchemesHitsPojoModel currentSchemeRow =  schemesListHitsWise.get(position);

out of else method

You need to change,

There are two chances to get this ERROR.

1.Change your

holder.schemeViewDate.setText(currentSchemeRow.getSchemeViewDate());
holder.schemeHits.setText(String.valueOf(currentSchemeRow.getSchemeHits()));

TO

   holder.schemeViewDate.setText(currentSchemeRow.get(position).getSchemeViewDate());
   holder.schemeHits.setText("" + currentSchemeRow..get(position).getSchemeHits());

currentSchemeRow should be Arraylist of your model where you have get and set data, and you have to get value from get(position) that is your selected position.

  1. Might have used the wrong id's OR have not declared any from both of layout files that is custom_recyclerview_row_each_scheme_hits OR custom_no_record_found_msg_screen

schemeName = (TextView) itemView.findViewById(R.id.hitsOnSchemeName);    
 languageName = (TextView) itemView.findViewById(R.id.hitsOnLangName);    
 viewIcon = (ImageView) itemView.findViewById(R.id.hitsOnViewIcon);    
 schemeViewDate=(TextView)itemView.findViewById(R.id.hitsOnSchemeViewDate); 
 schemeHits = (TextView) itemView.findViewById(R.id.hitsOnSchemeHits);

your problem is in onCreateViewHolder

    switch (success){
        case(0):
            view = layoutInflater.inflate(R.layout.custom_no_record_found_msg_screen, parent, false);
            break;
        case(1):
            view = layoutInflater.inflate(R.layout.custom_recyclerview_row_each_scheme_hits, parent,false);

here you inflating two different layouts so your all TexViews must be present in both layouts .

OR

place an if condition in onBindViewHolder to handle the null pointer exception because onBindViewHolder must be called in both cases.

First of all Thank you guys and i really appreciate your help. Actually it was my bad.

I was storing the value to success variable inside my onBindViewHolder() which is going to be used for inside both method (onCreateViewHolder(), onBindViewHolder() ) and that was my actual mistake

because i forgot that i am using this success variable in my onCreateViewHolder() as well to select my particular layout for particular situation and because of that mistake when my onCtreatViewHolder() runs, at that time success=0 and it returns the wrong layout.

Now the bottom line is this,

  1. I Create the Global variable named as success

  2. and inside onCreateViewHolder() Method i store the value for success

  3. then, i use this success variable for my both methods

here is my solution that is working fine currently.

 private int success;

    // Constructor
    public HitsOnSchemesDataDisplayAdapter(Context context){
        layoutInflater = LayoutInflater.from(context);
    }

    public void setSchemesListHitsWise(ArrayList<SchemesHitsPojoModel> schemesListHitsWise){
        this.schemesListHitsWise = schemesListHitsWise;
        notifyItemRangeChanged(0, schemesListHitsWise.size());
    }

onCreateViewHolder() Method

@Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        MyViewHolder viewHolder;
        SchemesHitsPojoModel firstObject = schemesListHitsWise.get(0);
        success = firstObject.getSuccess();
        switch (success){
            case(0):
                view = layoutInflater.inflate(R.layout.custom_no_record_found_msg_screen, parent, false);
                Log.d("VIEW "," is runned....");
                break;
            case(1):
                view = layoutInflater.inflate(R.layout.custom_recyclerview_row_each_scheme_hits, parent,false);
                Log.d("VIEW_2, "," is run....");
                break;

            default:
                view = layoutInflater.inflate(R.layout.custom_no_record_found_msg_screen, parent, false);
                break;
        }
        viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

onBindViewHolder() Method

 @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        SchemesHitsPojoModel firstObject = schemesListHitsWise.get(0);
        //int success = firstObject.getSuccess();
        if(success == 0 ){
            Log.d("SORRY!, "," We don't have any records to show you.");
        }
        else if(success == 1){
            SchemesHitsPojoModel currentSchemeRow =  schemesListHitsWise.get(position);
            holder.schemeName.setText(currentSchemeRow.getSchemeName());
            int langId = currentSchemeRow.getLanguage_id();
            switch(langId){
                case(1):
                    holder.languageName.setText("Punjabi");
                    break;
                case(2):
                    holder.languageName.setText("English");
            }

            holder.schemeViewDate.setText(currentSchemeRow.getSchemeViewDate());
            holder.schemeHits.setText(String.valueOf(currentSchemeRow.getSchemeHits()));
            holder.viewIcon.setImageResource(R.drawable.ic_remove_red_eye_black_18dp);
            //holder.totalHitsOnThisScheme.setText(currentSchemeRow.getSchemeTotalHitsCount());
        }
    }   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM