qqrrrr
qqrrrr

Reputation: 585

Failed to parse to snapshot: Firebase setValue ANDROID

firebase = new Firebase(Config.FIREBASE_URL_FARE);
        firebase.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.getChildrenCount() == 0) {
                    Fare fare = new Fare();
                    fare.setOrdinaryRegFirstFive(ordinaryRegularFF);
                    fare.setOrdinaryRegSucceeding(ordinaryRegSucceeding);
                    fare.setOrdinaryDiscountedFirstFive(ordinaryDisFF);
                    fare.setOrdinaryDiscountedSucceeding(ordinaryDisSucceeding);
                    fare.setAirconRegPerKm(airconRegPerKm);
                    fare.setAirconDiscountedPerKm(airconDisPerKm);
                    fare.setBusCompany(pref.getString("companyName", ""));
                    firebase.push().setValue(fare);

                    edtOrdinaryRegFF.setText("");
                    edtOrdinaryRegSucceeding.setText("");
                    edtOrdinaryDisFF.setText("");
                    edtOrdinaryDisSucceeding.setText("");
                    edtAirconRegPerKm.setText("");
                    edtAirconDisPerKm.setText("");
                    Toast.makeText(BusFareActivity.this, "Setting fare successfully.", Toast.LENGTH_LONG).show();
                }
 else {
                    for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                        if (postSnapshot.child("busCompany").exists() && postSnapshot.child("busCompany").equals(pref.getString("companyName", ""))) {
                            postSnapshot.getRef().child("ordinaryRegFirstFive").setValue(ordinaryRegularFF);
                            postSnapshot.getRef().child("ordinaryRegSucceeding").setValue(ordinaryRegSucceeding);
                            postSnapshot.getRef().child("ordinaryDiscountedFirstFive").setValue(ordinaryDisFF);
                            postSnapshot.getRef().child("ordinaryDiscountedSucceeding").setValue(ordinaryDisSucceeding);
                            postSnapshot.getRef().child("airconRegPerKm").setValue(airconRegPerKm);
                            postSnapshot.getRef().child("airconDiscountedPerKm").setValue(airconDisPerKm);

                            Toast.makeText(BusFareActivity.this, "Successfully updated the fare.", Toast.LENGTH_LONG).show();
                            check++;
                        }
                    }
                    if (check == 0) {
                        Fare fare = new Fare();
                        fare.setOrdinaryRegFirstFive(ordinaryRegularFF);
                        fare.setOrdinaryRegSucceeding(ordinaryRegSucceeding);
                        fare.setOrdinaryDiscountedFirstFive(ordinaryDisFF);
                        fare.setOrdinaryDiscountedSucceeding(ordinaryDisSucceeding);
                        fare.setAirconRegPerKm(airconRegPerKm);
                        fare.setAirconDiscountedPerKm(airconDisPerKm);
                        fare.setBusCompany(pref.getString("companyName", ""));
                        firebase.push().setValue(fare);

                        edtOrdinaryRegFF.setText("");
                        edtOrdinaryRegSucceeding.setText("");
                        edtOrdinaryDisFF.setText("");
                        edtOrdinaryDisSucceeding.setText("");
                        edtAirconRegPerKm.setText("");
                        edtAirconDisPerKm.setText("");
                        Toast.makeText(BusFareActivity.this, "Setting fare successfully.", Toast.LENGTH_LONG).show();
                        startActivity(new Intent(BusFareActivity.this, BusFareActivity.class));
                    }
                    check = 0;
                }

All data from Fare.java are double except the busCompany. I don't understand why it failed to parse to snapshot the setValue method.

public class Fare {

private double ordinaryRegFirstFive;
private double ordinaryRegSucceeding;
private double ordinaryDiscountedFirstFive;
private double ordinaryDiscountedSucceeding;
private double airconRegPerKm;
private double airconDiscountedPerKm;
private String busCompany;

public Fare() {}

public void setOrdinaryRegFirstFive(double ordinaryRegFirstFive) {
    this.ordinaryRegFirstFive = ordinaryRegFirstFive;
}

public void setOrdinaryRegSucceeding(double ordinaryRegSucceeding) {
    this.ordinaryRegSucceeding = ordinaryRegSucceeding;
}

public void setOrdinaryDiscountedFirstFive(double ordinaryDiscountedFirstFive) {
    this.ordinaryDiscountedFirstFive = ordinaryDiscountedFirstFive;
}

public void setOrdinaryDiscountedSucceeding(double ordinaryDiscountedSucceeding) {
    this.ordinaryDiscountedSucceeding = ordinaryDiscountedSucceeding;
}

public void setAirconRegPerKm(double airconRegPerKm) {
    this.airconRegPerKm = airconRegPerKm;
}

public void setAirconDiscountedPerKm(double airconDiscountedPerKm) {
    this.airconDiscountedPerKm = airconDiscountedPerKm;
}

public void setBusCompany(String busCompany) {
    this.busCompany = busCompany;
}
}

Firebase error:

com.firebase.client.FirebaseException: Failed to parse to snapshot

Upvotes: 1

Views: 218

Answers (1)

Sakshi Agarwal
Sakshi Agarwal

Reputation: 36

You are using private variables in your pojo class which might be a reason for the exception. When you try to push using a model class to firebase, the variables should be public.

Upvotes: 1

Related Questions