Null object reference when getting a GeoPoint from Cloud Firestore

435 Views Asked by At

In my MapsActivity, I am trying to add one marker for each record in my Cloud Firestore collection "Stores".

When I run the code, I get this FATAL EXCEPTION:

Attempt to invoke virtual method 'double com.google.firebase.firestore.GeoPoint.getLatitude()' on a null object reference

I don't know why I get null object reference. The path is correct (with the same path, I am extracting other data). The name of the field is correct (I have tried with many names: "position", "prova", "aaaaa", ecc). The field is correctly set.

I have also tried getLatitude

Here the details of my code.

MapsActivity.java

FirebaseFirestore.getInstance().collection("Stores")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                Store store = document.toObject(Store.class); 
                                GeoPoint gp = store.getPosition();
                                LatLng location = new LatLng(gp.getLatitude(), gp.getLongitude());

                                MarkerOptions options = new MarkerOptions()
                                        .position(location)
                                        .title(store.getName());

                                mMap.addMarker(options);
                            }
                        }
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.d("Error Listener",e.toString());
                    }
                });

Store.java

public class Store extends SugarRecord
{
    @Unique
    private String storeId;
    private GeoPoint position;

    public Store()
    {
        super();
    }


    public GeoPoint getPosition()
    {
        return position;
    }

    @Override
    public String toString()
    {
        return "Stores{" +
                "storeId='" + storeId + '\'' +
                ", position='" + position + '\'' +
                '}';
    }
}

Cloud Firestore

enter image description here

0

There are 0 best solutions below