0

so basically when I run my app, the app does not give me any errors however, when I tried open a page called 'Find Food Donations' in my app, it gave me this error code in the log:

'Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference' and also said that its Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference: 

at com.example.myddproject.FindFoodDonations.onCreate(FindFoodDonations.java:66).

Below is the code for the app where the error is occurring in the Java File (the line is bolded)

public class FindFoodDonations extends AppCompatActivity{
    private DatabaseReference ProductsRef;
    private RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;

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

        ProductsRef = FirebaseDatabase.getInstance().getReference().child("Products");

        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_organization_navigation);
        bottomNavigationView.setSelectedItemId(R.id.nav_find_donations);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.nav_find_donations:
                        return true;
                    case R.id.nav_organization_home:
                        startActivity(new Intent(getApplicationContext()
                                , HomeOrganization.class));
                        overridePendingTransition(0, 0);
                        return true;
                    case R.id.nav_organization_logout:
                        FirebaseAuth.getInstance().signOut();
                        Intent intosignup = new Intent(FindFoodDonations.this, MainActivity.class);
                        startActivity(intosignup);
                        return true;
                }
                return false;
            }
        });
        recyclerView = findViewById(R.id.recycler_menu);
        **recyclerView.setHasFixedSize(true);**
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

    }

    @Override
    protected void onStart() {
        super.onStart();

        FirebaseRecyclerOptions<Product> options =
                new FirebaseRecyclerOptions.Builder<Product>()
                        .setQuery(ProductsRef, Product.class)
                        .build();


        FirebaseRecyclerAdapter<Product, ProductViewHolder> adapter =
                new FirebaseRecyclerAdapter<Product, ProductViewHolder>(options) {
                    @Override
                    protected void onBindViewHolder(@NonNull ProductViewHolder productViewHolder, int i, @NonNull Product product) {
                        productViewHolder.txtProductName.setText(product.getPname());
                        productViewHolder.txtProductPrice.setText("Quantity = " + product.getQuantity() + " KG's");
                        Picasso.get().load(product.getImage()).into(productViewHolder.imageView);
                    }

                    @NonNull
                    @Override
                    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_items_layout, parent, false);
                        ProductViewHolder holder = new ProductViewHolder(view);
                        return holder;
                    }
                };
        recyclerView.setAdapter(adapter);
        adapter.startListening();
    }
}

The code for the XML File for the page above is:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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=".FindFoodDonations">
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bottom_organization_navigation"
        app:itemBackground="@color/design_default_color_on_primary"
        app:itemTextColor="@drawable/selector"
        app:itemIconTint="@drawable/selector"
        app:menu="@menu/organizationbottom_navigation"
        android:layout_alignParentBottom="true"/>

And a page that is linked (I dont know if its neccesary, but I'll just put it here)

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".HomeActivity">

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

</RelativeLayout>

I have not encountered such a error before so I don't know what to do.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

Try this

<androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_menu"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"/>

Instead of

<android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        >
    </android.support.v7.widget.RecyclerView>
Atik Faysal
  • 158
  • 1
  • 13
0

Cut the recyclerView.setHasFixedSize(true) and paste it below this code recyclerView.setLayoutManager(layoutManager). Hope it will work ?

  • RecyclerView object is null where you calling recyclerView.setHasFixedSize(true) this part of code make sure you initialize RecyclerView before callling it. – Deepak Shukla Nov 05 '20 at 12:18