I am working on a MapActivity
where I am getting supplier information from my database and storing it in Supplier
object. I am calling two functions fetchSupplier()
and FetchStationInfo()
where first one fetch supplier info and stores into Object and second one uses it. So, the problem is when I run the fetchSupplier()
alone the data gets fetched and stored in my object but, when I call both functions i.e FetchStationInfo()
after fetchSupplier()
it gives a null pointer exception in FetchStationInfo()
. Here's how I am calling the functions:
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
fetchSupplier();
fetchStationInfo();
}
Both of my functions:
private void fetchSupplier(){
String uid = mAuth.getUid();
mDatabase = FirebaseDatabase.getInstance().getReference()
.child("SUPPLIERS").child(uid);
mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
supplier = dataSnapshot.getValue(Supplier.class);
Log.i("USER FETCH COMPLETE ","["+supplier.getName()+"]");
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.i("FETCHING USER [ERROR] ",databaseError.getMessage());
}
});
}
private void fetchStationInfo(){
//Exception is here in this line where I am calling `getStation_id()`
mDatabase = FirebaseDatabase.getInstance().getReference()
.child("STATIONS").child(supplier.getStation_id());
mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
station = dataSnapshot.getValue(Station.class);
Log.i("STATION FETCH COMPLETE ","["+station.getName()+"]");
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.i("STATION FETCH [ERROR] ",databaseError.getMessage());
}
});
}
My objects are declared globally. Been working on it for hours but unable to fix it. My firebase structure: