Basically I'm trying to implement a Recycler View for a Tasks Fragment, and have followed what most people seem to have done that have worked for them but have come up with an issue where the myAdapter.startListening() method prompts this error message:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.student_productivity_and_reflection_app.tasks.TaskAdapter.startListening()' on a null object reference
I'm not sure what went wrong. Here is my Task class:
public class Task {
private String title, description;
private Date date;
public Task() {
//Empty constructor needed for firebase
}
public Task(String title, String description, Date date) {
this.title = title;
this.description = description;
this.date = date;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
Task Adapter:
public class TaskAdapter extends FirestoreRecyclerAdapter <Task, TaskAdapter.TaskHolder>{
public TaskAdapter(@NonNull FirestoreRecyclerOptions<Task> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull TaskHolder holder, int position, @NonNull Task model) {
holder.textViewTitle.setText(model.getTitle());
holder.textViewDescription.setText(model.getDescription());
holder.textViewDate.setText(String.valueOf(model.getDate()));
}
@NonNull
@Override
public TaskHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.task_items, viewGroup, false);
return new TaskHolder(v);
}
class TaskHolder extends RecyclerView.ViewHolder {
TextView textViewTitle;
TextView textViewDescription;
TextView textViewDate;
public TaskHolder(@NonNull View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.task_title);
textViewDescription = itemView.findViewById(R.id.task_description);
textViewDate = itemView.findViewById(R.id.task_date);
}
}
}
And TasksFragment:
public class TasksFragment extends Fragment{
private FirebaseAuth mAuth = FirebaseAuth.getInstance();
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private TaskAdapter myAdapter;
private FloatingActionButton newTask;
private RecyclerView recyclerView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tasks, container, false);
}
private void fetchTasks(View view){
Query query = db.collection("users").document("coolguy").collection("task").orderBy("date", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Task> options = new FirestoreRecyclerOptions.Builder<Task>().setQuery(query, Task.class).build();
myAdapter = new TaskAdapter(options);
recyclerView = view.findViewById(R.id.tasksList);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(myAdapter);
}
@Override
public void onStart() {
super.onStart();
myAdapter.startListening();
}
@Override
public void onStop() {
super.onStop();
myAdapter.stopListening();
}
}
( I know the date should be a Date variable with a proper format but I was planning to fix that once the functionality actually worked. )