TransWikia.com

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double

Stack Overflow Asked by Savindri on December 2, 2020

I want to multiply a value after retrieving it from the database(realtime firebase) and assign to another variable. I tried to do it like this, but I am getting an error. Can someone please help me?

 private void getData(final TextView deliCha) {
        rootRef.child(appId).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                double Distance = (double) dataSnapshot.child("Distance").getValue();
                double charge = Distance*100.0;
                deliCha.setText(String.valueOf(charge));
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

this the error.

    Process: com.example.ketomate, PID: 13156
    java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
        at com.example.ketomate.BillDetailsForPaymentAndDelivery$2.onDataChange(BillDetailsForPaymentAndDelivery.java:121)
        at com.google.firebase.database.Query$1.onDataChange(Query.java:179)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
        at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

3 Answers

"6.016539 Kilometers" string can not be converted to double because it has the word Kilometers. first you need to remove the word Kilometers before you convert to double.

private void getData(final TextView deliCha) {
    rootRef.child(appId).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            String distance = dataSnapshot.child("Distance").getValue();
            String[] separate = distance.split("Kilometers");
            double d = Double.parseDouble(separate[0]);
            double charge = d*100.0;
            deliCha.setText(String.valueOf(charge));
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

Correct answer by Ayodele Kayode on December 2, 2020

Without seeing the contents of your database, all I can conclude is that the value of a child called "Distance" is a string value, not a Double. You can't just cast a string to a double.

The first thing to do would be to fix the way you write values into the database. Instead of writing String values, convert them to doubles first, so that Firestore will return them to you as a double.

If you can't do that, you will have to convert the String to a double when you read it.

String distance = dataSnapshot.child("Distance").getValue(String.class);
double doubleDistance = Double.parseDouble(Distance);
double charge = doubleDistance * 100.0;
deliCha.setText(Double.toString(charge));

Answered by Doug Stevenson on December 2, 2020

You can use the Double.valueOf method

double Distance = Double.valueOf (dataSnapshot.child("Distance").getValue());

Answered by Scary Wombat on December 2, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP