Attempt to invoke virtual method ‘void androidx.recyclerview.widget.RecyclerView.setLayoutManager on a null object reference

Published
I am Facing this error please help me to resolve this error. Can anybody please resolve this issue. I need the solution as soon as possible. 

I’m not sure why but it comes up with the

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)’ on a null object reference
at com.example.mycart.CategoryActivity.onCreate(CategoryActivity.java:82)

I already tried adding the below lines because I saw that as a solution to another article that I read. Could anyone help me thanks

private RecyclerView categoryRecyclerView ;

----Code---

categoryRecyclerView = (RecyclerView) findViewById(R.id.category_recycleview);

---Code ---

final LinearLayoutManager testingLayoutManager = new LinearLayoutManager(this);
        testingLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        CategoryRecyclerView.setLayoutManager(testingLayoutManager);


Except this solution , anybody has different solution to solve this error : 

The below Code is CategoryActivity.java  :- 

```
package com.example.mycart;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

public class CategoryActivity extends AppCompatActivity {

    private RecyclerView categoryRecyclerView ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        String title = getIntent().getStringExtra("CategoryName");
        getSupportActionBar().setTitle(title);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        categoryRecyclerView = findViewById(R.id.category_recycleview);

        ///////////////  Banner Slider


        List<SliderModel>sliderModelList = new ArrayList<SliderModel>();

        sliderModelList.add(new SliderModel(R.mipmap.home_icon,"#077AE4"));
        sliderModelList.add(new SliderModel(R.mipmap.error,"#077AE4"));
        sliderModelList.add(new SliderModel(R.mipmap.greenemail_icon,"#077AE4"));

        sliderModelList.add(new SliderModel(R.mipmap.redemail_icon,"#077AE4"));
        sliderModelList.add(new SliderModel(R.mipmap.add_icon,"#077AE4"));
        //////   instead of add icon used to app icon
        sliderModelList.add(new SliderModel(R.mipmap.ic_launcher,"#077AE4"));
        sliderModelList.add(new SliderModel(R.mipmap.my_cart_icon,"#077AE4"));
        sliderModelList.add(new SliderModel(R.mipmap.default_profile_icon,"#077AE4"));
        sliderModelList.add(new SliderModel(R.mipmap.home_icon,"#077AE4"));

        /// add more banner list items if you want

        sliderModelList.add(new SliderModel(R.mipmap.error,"#077AE4"));
        sliderModelList.add(new SliderModel(R.mipmap.greenemail_icon,"#077AE4"));
        sliderModelList.add(new SliderModel(R.mipmap.redemail_icon,"#077AE4"));

        ///////////////  Banner Slider


        //////////////// Horizontal Product Layout

        List<HorizontalProductScrollModel> horizontalProductScrollModelList = new ArrayList<>();
        horizontalProductScrollModelList.add(new HorizontalProductScrollModel(R.mipmap.add_icon,"Redmi 5 A","SD Processor", "Rs 5999"));
        horizontalProductScrollModelList.add(new HorizontalProductScrollModel(R.mipmap.bell_icon,"Redmi 5 A","SD Processor", "Rs 5999"));
        horizontalProductScrollModelList.add(new HorizontalProductScrollModel(R.mipmap.greenemail_icon,"Redmi 5 A","SD Processor", "Rs 5999"));
        horizontalProductScrollModelList.add(new HorizontalProductScrollModel(R.mipmap.error,"Redmi 5 A","SD Processor", "Rs 5999"));
        horizontalProductScrollModelList.add(new HorizontalProductScrollModel(R.mipmap.home_icon,"Redmi 5 A","SD Processor", "Rs 5999"));
        horizontalProductScrollModelList.add(new HorizontalProductScrollModel(R.mipmap.redemail_icon,"Redmi 5 A","SD Processor", "Rs 5999"));
        horizontalProductScrollModelList.add(new HorizontalProductScrollModel(R.mipmap.my_orders,"Redmi 5 A","SD Processor", "Rs 5999"));
        horizontalProductScrollModelList.add(new HorizontalProductScrollModel(R.mipmap.reward_icon,"Redmi 5 A","SD Processor", "Rs 5999"));
        horizontalProductScrollModelList.add(new HorizontalProductScrollModel(R.mipmap.my_cart_icon,"Redmi 5 A","SD Processor", "Rs 5999"));
        horizontalProductScrollModelList.add(new HorizontalProductScrollModel(R.mipmap.greenemail_icon,"Redmi 5 A","SD Processor", "Rs 5999"));
        horizontalProductScrollModelList.add(new HorizontalProductScrollModel(R.mipmap.error,"Redmi 5 A","SD Processor", "Rs 5999"));

        //////////////  Horizontal Product Layout

        ////////////////////////////////////////////////////////


        LinearLayoutManager testingLayoutManager = new LinearLayoutManager(this);
        testingLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        categoryRecyclerView.setLayoutManager(testingLayoutManager);


        List<HomePageModel> homePageModelList = new ArrayList<>();
        homePageModelList.add(new HomePageModel(0,sliderModelList));
        homePageModelList.add(new HomePageModel(1,R.mipmap.banner,"#000000"));
        homePageModelList.add(new HomePageModel(2,"Deals of the Day",horizontalProductScrollModelList));
        homePageModelList.add(new HomePageModel(3,"Deals of the Day",horizontalProductScrollModelList));
        homePageModelList.add(new HomePageModel(1,R.mipmap.banner,"#ffff00"));
        homePageModelList.add(new HomePageModel(2,"Deals of the Day",horizontalProductScrollModelList));
        homePageModelList.add(new HomePageModel(3,"Deals of the Day",horizontalProductScrollModelList));
        homePageModelList.add(new HomePageModel(1,R.mipmap.banner,"#ff0000"));

        HomePageAdapter adapter = new HomePageAdapter(homePageModelList);
        categoryRecyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.search_icon, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        int id=item.getItemId();
        if (id == R.id.main_search_icon){
            // todo : search
            return true;
        }
        else if (id == android.R.id.home){
            finish();
            return true ;
        }
        return super.onOptionsItemSelected(item);
    }

}


```
The below Code is activity_category.xml  :- 

```

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CategoryActivity">


    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.mycart.AppBarOverlay"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/Theme.mycart.PopupOverlay" />


    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/category_recyclerview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/appBarLayout" />

</androidx.constraintlayout.widget.ConstraintLayout>


```

Source: Android Questions

Published
Categorized as android, android-recyclerview

Answers

please bhai mera same problem hai solution bta do please please


manish chaurasiya

Leave a Reply

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq