I followed this guide How to display data from Firestore in a RecyclerView with Android?, but am facing a NullPointerException in the setter in the ViewHolder. I'm also actually very unsure of what to do in the setter in the ViewHolder...
![database]https://i.stack.imgur.com/yiYaO.jpg
Model:
public class FoodModel {
private String foodName;
private String foodType;
private String foodQty;
private String foodExpDate;
public FoodModel() {}
public FoodModel(String name, String type, String qty, String expDate) {
this.foodName = name;
this.foodType = type;
this.foodQty = qty;
this.foodExpDate = expDate;
}
public String getFoodName() {return foodName;}
public void setFoodName(String name) {foodName = name;}
public String getFoodType() {return foodType;}
public void setFoodType(String type) {foodType = type;}
public String getFoodQty() {return foodQty;}
public void setFoodQty(String qty) {foodQty = qty;}
public String getFoodExpDate() {return foodExpDate;}
public void setFoodExpDate(String expDate) {foodExpDate = expDate;}
}
Activity code:
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
Query foodQuery = db.collection("Food")
.orderBy("food_name", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<FoodModel> options = new FirestoreRecyclerOptions.Builder<FoodModel>()
.setQuery(foodQuery, FoodModel.class)
.build();
adapter = new FirestoreRecyclerAdapter<FoodModel, FoodViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull FoodViewHolder holder, int position, @NonNull FoodModel model) {
holder.setFoodName(model.getFoodName());
}
@NonNull
@Override
public FoodViewHolder onCreateViewHolder(@NonNull ViewGroup group, int viewType) {
View view = LayoutInflater.from(group.getContext()).inflate(R.layout.recycler_view_row, group, false);
return new FoodViewHolder(view);
}
};
recyclerView.setAdapter(adapter);
}
private class FoodViewHolder extends RecyclerView.ViewHolder {
private View view;
FoodViewHolder(View itemView) {
super(itemView);
view = itemView;
}
void setFoodName(String foodName) {
TextView textView = view.findViewById(R.id.editName);
textView.setText(foodName);
}
}
xml of recycler_view_row:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/foodListRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
errors:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.qremind, PID: 17735
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.qremind.FoodList$FoodViewHolder.setFoodName(FoodList.java:91)
at com.example.qremind.FoodList$1.onBindViewHolder(FoodList.java:70)
at com.example.qremind.FoodList$1.onBindViewHolder(FoodList.java:67)
at com.firebase.ui.firestore.FirestoreRecyclerAdapter.onBindViewHolder(FirestoreRecyclerAdapter.java:158)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1897)
at androidx.recyclerview.widget.RecyclerView$1.run(RecyclerView.java:414)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:878)
at android.view.Choreographer.doCallbacks(Choreographer.java:690)
at android.view.Choreographer.doFrame(Choreographer.java:622)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:864)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:185)
at android.app.ActivityThread.main(ActivityThread.java:6473)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:916)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:806)