0

Error code:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: konak.labs.warstoriesv2, PID: 20309
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at konak.labs.warstoriesv2.exampleAuthorsAdapter.onBindViewHolder(exampleAuthorsAdapter.java:61)
    at konak.labs.warstoriesv2.exampleAuthorsAdapter.onBindViewHolder(exampleAuthorsAdapter.java:17)
    at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
    at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
    at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
    at androidx.recyclerview.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:557)
    at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
    at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
    at androidx.recyclerview.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
    at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at com.android.internal.policy.DecorView.onLayout(DecorView.java:758)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2484)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2200)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
    at android.view.Choreographer.doCallbacks(Choreographer.java:723)
    at android.view.Choreographer.doFrame(Choreographer.java:658)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

I keep getting this error. I am working with Androidx RecyclerView.

I have 3 activities/classes- activityAuthor, exampleAuthorsAdapter and exampleItemAuthors.

I am sure that the error is caused because of the adapter or activity. I am trying to populate a recyclerview layout file with the data from the files mentioned above.

Here is the code: exampleAuthorsAdapter:

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

public class exampleAuthorsAdapter extends RecyclerView.Adapter<exampleAuthorsAdapter.MyViewHolder> {

    private Context mContext;
    private ArrayList<exampleItemAuthors> mExampleListAuthors;

    static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView authorTitle;
        ImageView authorThumbnail;
        CardView cardview;

        MyViewHolder(View itemView) {
            super(itemView);

            authorTitle = itemView.findViewById(R.id.authorCardViewTitle);
            authorThumbnail = itemView.findViewById(R.id.authorCardViewImage);
            cardview = itemView.findViewById(R.id.cardViewMainAuthors);
        }
    }

    exampleAuthorsAdapter(Context mContext, ArrayList<exampleItemAuthors> exampleItemAuthors) {

        mExampleListAuthors = exampleItemAuthors;
        this.mContext = mContext;

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_authors, parent, false);
        RecyclerView.ViewHolder holder = new RecyclerView.ViewHolder(view) {

        };
        return new MyViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {

        exampleItemAuthors currentItem = mExampleListAuthors.get(position);
        Context montext = holder.itemView.getContext();

        holder.authorTitle.setText(currentItem.getAuthorTitle());
        holder.authorThumbnail.setImageResource(currentItem.getAuthorThumbnail());
        holder.cardview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, activityAuthorsView.class);
                intent.putExtra("Title", mExampleListAuthors.get(position).getAuthorTitle());
                intent.putExtra("Description", mExampleListAuthors.get(position).getAthorDescription());
                intent.putExtra("Thumbnail", mExampleListAuthors.get(position).getAuthorThumbnail());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {

        return mExampleListAuthors.size();

    }
}

exampleItemAuthor:

public class exampleItemAuthors {

private String mAuthorTitle;
private String mAuthorDescription;
private int mAuthorThumbnail;

exampleItemAuthors(String authorTitle, String authorDescription, int authorThumbnail) {

    mAuthorTitle = authorTitle;
    mAuthorDescription = authorDescription;
    mAuthorThumbnail = authorThumbnail;

}
public void setmAuthorTitle(String mAuthorTitle) {
    this.mAuthorTitle = mAuthorTitle;
}

public void setmAuthorDescription(String mAuthorDescription) {
    this.mAuthorDescription = mAuthorDescription;
}

public void setmAuthorThumbnail(int mAuthorThumbnail) {
    this.mAuthorThumbnail = mAuthorThumbnail;
}

String getAuthorTitle() {
    return mAuthorTitle;
}

String getAthorDescription() {
    return mAuthorDescription;
}

int getAuthorThumbnail() {
    return mAuthorThumbnail;
}

}

and activityAuthor:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class activityAuthors extends AppCompatActivity {

    RecyclerView.LayoutManager mLayoutManagerAuthor = new GridLayoutManager(this, 2);

    public void onCreate(Bundle savedInstanceState) {  //  This works for showing the layout.
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_authors);

        ArrayList<exampleItemAuthors> listAuthors = new ArrayList<>();
        RecyclerView.Adapter mAdapterAuthor = new exampleAuthorsAdapter(this, listAuthors);
        RecyclerView mRecyclerViewAuthor = findViewById(R.id.recyclerViewAuthor);
        mRecyclerViewAuthor.setAdapter(mAdapterAuthor);
        mRecyclerViewAuthor.setLayoutManager(mLayoutManagerAuthor);

        mRecyclerViewAuthor.setHasFixedSize(true);

        listAuthors.add(new exampleItemAuthors(data here);
}
}

Any help guys? I tried everything i could think of but i am new to this.

Vanguard
  • 13
  • 11
  • 1
    In your `Adapter`'s `onCreateViewHolder()` method, you're inflating the `activity_authors` layout, but that's your `Activity`'s layout, Instead, you need to inflate the layout for the `RecyclerView` items. Also, in the future, please provide the complete stack trace when asking about crashes, rather than just the first line. – Mike M. Apr 05 '20 at 22:11
  • That is my layout name. I should have changed it to something better but to clarify, i checked the reference youre saying and it is the correct layout im imflating, i just named it badly. This layout has the recyclerview too so it should work... – Vanguard Apr 05 '20 at 22:12
  • In the `inflate(R.layout.activity_authors, parent, false)` call there, you need to change `R.layout.activity_authors` to the `R.layout` for the `RecyclerView` items; i.e., whichever layout contains `authorCardViewTitle`, `authorCardViewImage`, and `cardViewMainAuthors`. – Mike M. Apr 05 '20 at 22:15
  • I added the error stack trace. The error points to the adapter. – Vanguard Apr 05 '20 at 22:16
  • Yeah, in this case, you have only one `setText()` call, so it was easy to pin down, but it's not always so. – Mike M. Apr 05 '20 at 22:19
  • How to fix it?> – Vanguard Apr 05 '20 at 22:21
  • Change `R.layout.activity_authors` in the aforementioned `inflate()` call to `R.layout.whatever_you_named_the_recycler_view_item_layout`. – Mike M. Apr 05 '20 at 22:23
  • 1
    that worked! so we have to explicitly name the recyclerview layout id for the adapter onCreateViewHolder() method! – Vanguard Apr 05 '20 at 22:39
  • Yep, because in the `Adapter`, you're creating the individual items, not the `Activity` again. Glad you got it working. Cheers! – Mike M. Apr 05 '20 at 22:46

0 Answers0