iGeni
iGeni

Reputation: 61

java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.util.Map

I am getting the error in this line: "Map map = (Map) dataSnapshot.getValue();"

     private void getAssignedCustomer(){
    String driverId = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference assignedCustomerRef = FirebaseDatabase.getInstance().getReference().child("Users").child("Drivers").child(driverId);
    assignedCustomerRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){

                Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();

                if(map.get("customerRideId") != null){
                    customerId = map.get("customerRideId").toString();
                    getAssignedCustomerPickupLocation();
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}

How to get rid of this error???

Upvotes: 2

Views: 5649

Answers (4)

Alucard
Alucard

Reputation: 5

I hope you solved the problem, if not, use this code:

private void getAssignedCustomer(){
String driverId = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference assignedCustomerRef = 
FirebaseDatabase.getInstance().getReference().child("Users")
 .child("Drivers").child(driverId).child("customerRideId");
assignedCustomerRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if(dataSnapshot.exists()){
             customerId = dataSnapshot.getValue().toString();
             getAssignedCustomerPickupLocation();
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
    }
});

}

Upvotes: 0

Pradeep Goyal
Pradeep Goyal

Reputation: 1

Please check this code

if (dataSnapshot.getValue() != null) {
  String avataStr = (String) dataSnapshot.getValue();
}

Upvotes: 0

Razor
Razor

Reputation: 153

First check in your firabase database that the value for the key driverId that you are trying to retrieve under your node Users/Drivers is not a boolean.

Upvotes: 0

gic186
gic186

Reputation: 836

Quoting from the Firebase documentation:

getValue() returns the data contained in this snapshot as native types. The possible types returned are:

Boolean

String

Long

Double

Map

List

This list is recursive; the possible types for Object in the above list is given by the same list. These types correspond to the types available in JSON.

The value returned from dataSnapshot.getValue() seems to be Boolean, and it cannot certainly be assigned to a Map<String, Object>.

Make sure that you are using the correct data from dataSnapshot

Upvotes: 1

Related Questions