-2

There is something I'm missing when I'm setting RecyclerView to my adapter. The App crashes when running it. Can anyone please help me with identifying the issue?

// My adapter
public class Adapter extends RecyclerView.Adapter<Adapter.myViewHolder> {

    private Context mContext;
    private List<item> mData;

    public Adapter(Context mContext, List<item> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @Override
    public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.card_item, parent, false);
        return new myViewHolder(v);
    }

    @Override
    public void onBindViewHolder(myViewHolder holder, int position) {
        holder.background_img.setImageResource(mData.get(position).getBackground());
        holder.profilePhoto.setImageResource(mData.get(position).getProfilePhoto());
        holder.tv_title.setText(mData.get(position).getProfileName());
        holder.tv_nbFollowers.setText(mData.get(position).getNbFollower() + " Followers");
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public static class myViewHolder extends RecyclerView.ViewHolder {

        ImageView profilePhoto, background_img;
        TextView tv_title, tv_nbFollowers;

        public myViewHolder(View itemView) {
            super(itemView);
            profilePhoto = itemView.findViewById(R.id.profile_img);
            background_img = itemView.findViewById(R.id.card_background);
            tv_title = itemView.findViewById(R.id.card_title);
            tv_nbFollowers = itemView.findViewById(R.id.card_nb_follower);
        }
    }
}

And this is how I set the RecyclerView with the adapter

RecyclerView recList = findViewById(R.id.rv_list);
recList.setHasFixedSize(true);
List<item> mlist = new ArrayList<>();
mlist.add(new item(R.drawable.fish0, "Bass", R.drawable.profile0, 2500));
mlist.add(new item(R.drawable.fish1, "Mondo Bass", R.drawable.profile1, 3500));
mlist.add(new item(R.drawable.fish2, "Large Mouth Bass", R.drawable.profile2, 5500));
mlist.add(new item(R.drawable.fish3, "Bass", R.drawable.profile3, 10500));
Adapter adapter = new Adapter(this, mlist);
recList.setAdapter(adapter);
recList.setLayoutManager(new LinearLayoutManager(this));

Here is the Logcat error

2018-10-19 19:17:20.479 10635-10635/fishingfreaks.ffapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: fishingfreaks.ffapp, PID: 10635
    java.lang.RuntimeException: Unable to start activity ComponentInfo{fishingfreaks.ffapp/fishingfreaks.ffapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
        at fishingfreaks.ffapp.MainActivity.onCreate(MainActivity.java:31)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Cliff Nees
  • 11
  • 3
  • What specifically goes wrong? – Stuart Whitehouse Oct 19 '18 at 23:14
  • The app crashes when running it. – Cliff Nees Oct 19 '18 at 23:14
  • Crashes? Do you have a debugger that shows you the error and where it happens? Does it just exit silently? Stack Overflow isn't just a debugging service - you'll need a more specific question to get a good answer. – Stuart Whitehouse Oct 19 '18 at 23:16
  • I updated the question with more details from the Logcat. The app exits silently. – Cliff Nees Oct 19 '18 at 23:20
  • **java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference** your logcat already specified the problem. Your recyclerview is null. And you're trying to setHasFixedSize(true) on that recyclerview. Check your activity xml that id of recyclerview is specifically there. – Al-Amin Oct 20 '18 at 03:09
  • Downvoting as logs clearly shows where and what the problem is. Learn to debug and understand logs. Ref : https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – Gaurav Chauhan Oct 20 '18 at 05:00
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Reaz Murshed Oct 20 '18 at 08:09
  • Can you please share the whole code from the `onCreate` function? – Reaz Murshed Oct 20 '18 at 08:09
  • Thank you everyone for your feedback. The issue is solved. – Cliff Nees Oct 20 '18 at 14:45

2 Answers2

1

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference at fishingfreaks.ffapp.MainActivity.onCreate(MainActivity.java:31) at

From the logs it is clear that you are setting recyclerView.setHasFixedSize() at a point where instance of your recylerview is null. See line 31 of your MainActivity.

Gaurav Chauhan
  • 376
  • 3
  • 14
0

You are getting a NullPointerException. So the layout item that you are referring to, is not found while you are trying to call the setHasFixedSize function on it.

This might happen for two reasons.

  • You might have referred to the wrong layout item id. Double check the id rv_list. Does it has the same spelling in your layout as well?
  • If the above condition is okay (i.e. your layout does have the rv_list as the id of your RecyclerView, then you might need to check if you have set the content view in your activity's onCreate function. Do you have the setContentView function call in your onCreate function? If not, then add the setContentView(R.layout.your_layout_with_recyclerview) as the first statement after the super call of your onCreate function.
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98