I'm creating an app that shows the data in recyclerview.
when I run the app this line of error shows up and I'm having trouble fixing it
2022-04-28 21:36:59.212 12509-12509/com.example.chocolate_republic E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chocolate_republic, PID: 12509
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.example.chocolate_republic.adapters.UpdateProductAdapter.getItemCount(UpdateProductAdapter.java:70)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:4214)
at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3680)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
here is my code in the main activity
firestore = FirebaseFirestore.getInstance();
update_rec = findViewById(R.id.admin_update_rec);
update_rec.setLayoutManager(new LinearLayoutManager(this,RecyclerView.VERTICAL,false));
Query query = FirebaseFirestore.getInstance().collection("AllProducts")
.orderBy("productName",Query.Direction.ASCENDING)
.limit(10);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()){
updateProductModelList = new ArrayList<>();
for (DocumentSnapshot documentSnapshot : task.getResult()){
UpdateProductModel updateProductModel = documentSnapshot.toObject(UpdateProductModel.class);
updateProductModelList.add(updateProductModel);
}
updateProductAdapter = new UpdateProductAdapter(updateProductModelList);
update_rec.setAdapter(updateProductAdapter);
lastVisible = task.getResult().getDocuments().get(task.getResult().size()-1);
RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL){
isScrolling = true;
}
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition() ;
int visibleItemCount = linearLayoutManager.getChildCount() ;
int totalItemCount = linearLayoutManager.getItemCount () ;
if (isScrolling && (firstVisibleItem + visibleItemCount == totalItemCount) && !isLastItemReached){
isScrolling = false;
Query nextQuery = FirebaseFirestore.getInstance()
.collection("AllProducts")
.orderBy("productName",Query.Direction.ASCENDING)
.startAfter(lastVisible)
.limit(10);
nextQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for (DocumentSnapshot documentSnapshot : task.getResult()){
UpdateProductModel updateProductModel = documentSnapshot.toObject(UpdateProductModel.class);
updateProductModelList.add(updateProductModel);
}
updateProductAdapter.notifyDataSetChanged();
lastVisible = task.getResult().getDocuments().get(task.getResult().size()-1);
Toast.makeText(AdminUpdateProductActivity.this, "Next Page Loaded", Toast.LENGTH_SHORT).show();
if (task.getResult().size() < 10){
isLastItemReached = true;
}
}
});
}
}
};
update_rec.addOnScrollListener(onScrollListener);
}
}
});
the place where the error is showing me return updateProductModelList.size();
public class UpdateProductAdapter extends RecyclerView.Adapter<UpdateProductAdapter.ViewHolder> {
Context context;
List<UpdateProductModel> updateProductModelList;
public UpdateProductAdapter(Context context, List<UpdateProductModel> updateProductModelList) {
this.context = context;
this.updateProductModelList = updateProductModelList;
}
public UpdateProductAdapter(List<UpdateProductModel> updateProductModelList) {
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.update_product,parent,false));
}
@Override
public void onBindViewHolder(@NonNull UpdateProductAdapter.ViewHolder holder, int position) {
Glide.with(context).load(updateProductModelList.get(position).getImg_url()).into(holder.imageView);
holder.name.setText(updateProductModelList.get(position).getProductName());
holder.price.setText(String.valueOf(updateProductModelList.get(position).getPrice()));
holder.stock.setText(String.valueOf(updateProductModelList.get(position).getStock()));
holder.category.setText(updateProductModelList.get(position).getType());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, UpdateDetail.class);
intent.putExtra("update",updateProductModelList.get(position));
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return updateProductModelList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView name,price,stock,category;
public ViewHolder(@NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.update_Item_Name);
price = itemView.findViewById(R.id.update_Item_price);
stock = itemView.findViewById(R.id.update_stock);
category = itemView.findViewById(R.id.update_category);
imageView = itemView.findViewById(R.id.update_Item_Img);
}
}
}
could someone help me fix the error that I am currently getting?