• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Valu Event listener being skipped for firebase database

I get an error in the requests adapter saying list.size = null. When debugging, I noticed the value event listener is not being triggered and it jumps directly to the adapter = new request adapter I have used the same instantiation of the database reference in other parts of the code and it works so I am not sure why this is happening? help please! :(

public class Requests extends Fragment {


RecyclerView acted, unseen;
RecyclerView.Adapter adapter, adapter2;
ArrayList<Requestitem> requestitems;


public Requests() {
// Required empty public constructor
}
@override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_requests, container, false);

}
@override
public void onViewCreated(View view, @nullable Bundle savedInstanceState)
{
unseen = getView().findViewById(R.id.recunseen);
acted = getView().findViewById(R.id.reqacted);

unseen.setLayoutManager(new LinearLayoutManager(this.getContext()));
acted.setLayoutManager(new LinearLayoutManager(this.getContext()));
DatabaseReference userefdata = FirebaseDatabase.getInstance().getReference().child("Requests");
userefdata.addValueEventListener(new ValueEventListener() {

@override
public void onDataChange(@NonNull DataSnapshot snapshot) {


for (DataSnapshot snap : snapshot.getChildren()) {

Requestitem evt = new Requestitem(snap.child("name").getValue(String.class),
snap.child("major").getValue(String.class),
snap.child("time").getValue(String.class),
snap.child("riderid").getValue(String.class),
snap.child("driverid").getValue(String.class),
false
);

requestitems.add(evt);
}
adapter.notifyDataSetChanged();

}

@override
public void onCancelled(@NonNull DatabaseError error) {

}

});
adapter2 = new RequestAdapter(requestitems, new RequestAdapter.OnItemClickListener() {
@override
public void OnItemClick(Requestitem ev) {
showselected(new Profile(ev.getRiderid()));
}
});

adapter = new RequestAdapter(requestitems, new RequestAdapter.OnItemClickListener() {
@override
public void OnItemClick(Requestitem ev) {
showselected(new Profile(ev.getRiderid()));
}
});
acted.setAdapter(adapter2);
unseen.setAdapter(adapter);


}





private void showselected(Fragment f){

getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.cont, f)

.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)

.commit();

}
}
 
It looks like the issue is with the requestitems ArrayList, which is being used to store the Requestitem objects that are retrieved from the database. This ArrayList is being passed to the RequestAdapter as the data source, but it has not been initialized.

To fix this, you can initialize the requestitems ArrayList before the ValueEventListener is added. This will ensure that the ArrayList is not null when the data is being added to it in the onDataChange() method.

Try adding this line at the beginning of the onViewCreated() method:

Code:
requestitems = new ArrayList<>();

This will create a new empty ArrayList and assign it to the requestitems variable. The ValueEventListener will then be able to add items to the ArrayList without encountering a null pointer exception.

I hope this helps! Let me know if you have any questions or if you need further assistance.
 
Back
Top Bottom