0

I am trying to get locations using geoFire and place the marker on a map on that location but geoFire.getLocation() is not running. I tried putting some check Logs to see where the code is going wrong and found that after running log OKAY 1 and Log key the code is jumping directly to log OKAY 3. Here is the result of my logcat:

/supplier.watersystem.com.suppliersideapp I/OKAY: Okay 1 /supplier.watersystem.com.suppliersideapp I/KEY FOUND: station_0 /supplier.watersystem.com.suppliersideapp I/OKAY: Okay 3
11-03 19:10:25.966 32678-32678/supplier.watersystem.com.suppliersideapp E/AndroidRuntime: FATAL EXCEPTION: main Process: supplier.watersystem.com.suppliersideapp, PID: 32678

Even the id I am getting is right. I am getting NullPointerException where I am placing the marker because my location ArrayList is going empty which must have some value because if there is some error getting a location it must log it otherwise it must put the location from the database or a 0,0 LatLng.

Here's my code:

     //reading station data from database and adding marker
    mDatabase = FirebaseDatabase.getInstance().getReference().child("STATIONS");
    ValueEventListener stationListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            int i=0;
            DatabaseReference geoFireRef = FirebaseDatabase.getInstance().getReference().child("STATION_LOCATIONS");
            GeoFire geoFire = new GeoFire(geoFireRef);
            for(DataSnapshot stationsSnapshot: dataSnapshot.getChildren()){
                stations.add(stationsSnapshot.getValue(Station.class));
                //Fetching Location
                Log.i("OKAY ", "Okay 1");
                Log.i("KEY FOUND ", stationsSnapshot.getKey());
                geoFire.getLocation(stationsSnapshot.getKey(), new LocationCallback() {
                    @Override
                    public void onLocationResult(String key, GeoLocation location) {
                        Log.i("OKAY ", "Okay 2");
                        if(location!=null){
                            Log.i("OKAY ", "Okay 2.1 not null");
                            stationLocations.add(new LatLng(location.latitude,location.longitude));
                        }
                        else{
                            Log.i("OKAY ", "Okay 2.2 null");
                            stationLocations.add(new LatLng(0,0));
                        }
                    }
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Log.i("Location Read [ERROR] ",databaseError.getMessage());
                    }
                });
                //placing marker
                Log.i("OKAY ", "Okay 3");
                mMap.addMarker(new MarkerOptions().position(stationLocations.get(i)).title(stations.get(i).getName()));
                Log.i("OKAY ", "Okay 4");
                i++;
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.i("Station Read [ERROR]",databaseError.getMessage());
        }
    };
    mDatabase.addListenerForSingleValueEvent(stationListener);

The structure of my database: enter image description here

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Hussam
  • 1,606
  • 9
  • 20
  • Please check the duplicate to see why do you have this behaviour and how can you solve this using a custom callback. – Alex Mamo Nov 04 '18 at 07:46

1 Answers1

1

I think it do not work because your okay2 part is async and it do not have enough time to get the answer because the okay3 part dont wait it.