Hi experts I am new to android studio and currently watching tutorials. Right now I encountered
'java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
error even after following the tutorial. I've also searched for same issue and found that this can be solved by initializing the recyclerview and providing the right layout name. I'm pretty sure I've met both solution but I am still facing the same issue. Please guide me on this.
MainActivity.java
package com.example.cart;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.SearchView;
import com.example.cart.adapter.MyProductAdapter;
import com.example.cart.listener.CartLoadListener;
import com.example.cart.listener.ProductLoadListener;
import com.example.cart.model.CartModel;
import com.example.cart.model.ProductModel;
import com.example.cart.utils.SpaceItemDecoration;
import com.google.android.material.snackbar.Snackbar;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.nex3z.notificationbadge.NotificationBadge;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity implements ProductLoadListener, CartLoadListener {
@BindView(R.id.rv)
RecyclerView rviewer;
@BindView(R.id.mainLayout)
RelativeLayout mainLayout;
@BindView(R.id.badge)
NotificationBadge badge;
@BindView(R.id.btnCart)
FrameLayout btnCart;
@BindView(R.id.search)
SearchView search;
ProductLoadListener productLoadListener;
CartLoadListener cartLoadListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
loadProductFromFirebase();
}
private void loadProductFromFirebase() {
List<ProductModel> productModels = new ArrayList<>();
FirebaseDatabase.getInstance()
.getReference("product")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.exists()) {
for(DataSnapshot productSnapshot:snapshot.getChildren()){
ProductModel productModel = productSnapshot.getValue(ProductModel.class);
productModel.setKey(productSnapshot.getKey());
productModels.add(productModel);
}
productLoadListener.onProductLoadSuccess(productModels);
} else {
productLoadListener.onProductLoadFail("Can't Find Product");
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
productLoadListener.onProductLoadFail(error.getMessage());
}
});
}
private void init() {
ButterKnife.bind(this);
productLoadListener = this;
cartLoadListener = this;
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
rviewer.setLayoutManager(gridLayoutManager);
rviewer.addItemDecoration(new SpaceItemDecoration());
}
@Override
public void onProductLoadSuccess(List<ProductModel> productModelList) {
MyProductAdapter adapter = new MyProductAdapter(this, productModelList);
rviewer.setAdapter(adapter);
}
@Override
public void onProductLoadFail(String message) {
Snackbar.make(mainLayout, message, Snackbar.LENGTH_LONG).show();
}
@Override
public void onCartLoadSuccess(List<CartModel> cartModelList) {
}
@Override
public void onCartLoadFail(String message) {
}
}
MyProductAdapter.java
package com.example.cart.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.cart.R;
import com.example.cart.model.ProductModel;
import com.makeramen.roundedimageview.RoundedImageView;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
public class MyProductAdapter extends RecyclerView.Adapter<MyProductAdapter.MyProductViewHolder> {
private Context context;
private List<ProductModel> productModelList;
public MyProductAdapter(Context context, List<ProductModel> productModelList) {
this.context = context;
this.productModelList = productModelList;
}
@NonNull
@Override
public MyProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new MyProductViewHolder(LayoutInflater.from(context)
.inflate(R.layout.product_item, parent, false));
}
@Override
public void onBindViewHolder(@NonNull MyProductViewHolder holder, int position) {
Glide.with(context)
.load(productModelList.get(position).getImage())
.into(holder.img1);
holder.productPrice.setText(new StringBuilder("$").append(productModelList.get(position).getPrice()));
holder.productName.setText(new StringBuilder().append(productModelList.get(position).getName()));
holder.productDescription.setText(new StringBuilder().append(productModelList.get(position).getDescription()));
}
@Override
public int getItemCount() {
return productModelList.size();
}
public class MyProductViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.img1)
RoundedImageView img1;
@BindView(R.id.productName)
TextView productName;
@BindView(R.id.productDescription)
TextView productDescription;
@BindView(R.id.productPrice)
TextView productPrice;
private Unbinder unbinder;
public MyProductViewHolder(@NonNull View itemView) {
super(itemView);
unbinder = ButterKnife.bind(this, itemView);
}
}
}
activity_main.xml
<?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:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3BE838"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/searchCartLayout"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:orientation="horizontal"
android:weightSum="16"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0">
<TextView
android:id="@+id/search"
android:layout_width="0dp"
android:layout_weight="11"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:textStyle="bold"
android:textSize="30sp"/>
<androidx.appcompat.widget.SearchView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
android:layout_weight="2"
android:src="@drawable/ic_baseline_search_24"/>
<FrameLayout
android:id="@+id/btnCart"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="match_parent">
<ImageView
android:id="@+id/cartButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_baseline_shopping_cart_24" />
<com.nex3z.notificationbadge.NotificationBadge
android:id="@+id/badge"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
app:nbMaxTextLength="2"/>
</FrameLayout>
</LinearLayout>
<TextView
android:id="@+id/breadCrumb"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="@drawable/rounded_corner_view"
android:padding="2dp"
android:text=" App/Page name : Products / Services"
app:layout_constraintBottom_toTopOf="@id/rv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.6" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="750dp"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/breadCrumb"
app:layout_constraintVertical_bias="1.0" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1"
app:menu="@menu/bottom_nav_menu" />
</RelativeLayout>