Home > Enterprise >  Can't convert object of type java.lang.Double to type com.newapp.elephantapplication.Data_Model
Can't convert object of type java.lang.Double to type com.newapp.elephantapplication.Data_Model

Time:04-03

I'm trying to connect my app to a Firebase Database, but whenever I launch the code that problem shows up

that's my "LiveLocationActivity" activity

public class LiveLocationActivity extends AppCompatActivity implements OnMapReadyCallback {    
    RecyclerView recyclerView ;
    DatabaseReference db;
    Data_Adapter data_adapter ;
    ArrayList<Data_Model> list ;

    private GoogleMap mMap;
    private ActivityMapsBinding binding;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_location_acivity);

        recyclerView = findViewById(R.id.eleList);
        db = FirebaseDatabase.getInstance().getReference("GPS");
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map_frag);
        mapFragment.getMapAsync(this);    

        list = new ArrayList<>();
        data_adapter  = new  Data_Adapter(this,list);
        recyclerView.setAdapter(data_adapter);
        db.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for(DataSnapshot dataSnapshot : snapshot.getChildren())
                {
                    Data_Model data_model = dataSnapshot.getValue(Data_Model.class);
                    list.add(data_model);
                }
                data_adapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {    
            }
        });    
    }

    @Override
    public void onMapReady(@NonNull GoogleMap googleMap) {
        mMap = googleMap;

        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("GPS");

        ValueEventListener listener = databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                Double f_latitude = snapshot.child("f_latitude").getValue(Double.class);
                Double f_longitude = snapshot.child("f_longitude").getValue(Double.class);

//                Double f_latitude = 28.6139391;
//                Double f_longitude = 77.2068325 ;

                LatLng location = new LatLng(f_latitude,f_longitude);

                mMap.addMarker(new MarkerOptions().position(location).title("ELE00001"));
                mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {    
            }
        });
    }
}

that's my Data Model class

public class Data_Model {
    String IdElephant,Daily_Log_Date;
    long f_latitude,f_longitude;

    public Data_Model(String idElephant, String daily_Log_Date, long f_latitude, long f_longitude) {
        IdElephant = idElephant;
        Daily_Log_Date = daily_Log_Date;
        this.f_latitude = f_latitude;
        this.f_longitude = f_longitude;
    }

    public String getIdElephant() {
        return IdElephant;
    }

    public String getDaily_Log_Date() {
        return Daily_Log_Date;
    }

    public long getF_latitude() {
        return f_latitude;
    }

    public long getF_longitude() {
        return f_longitude;
    }

    public void setIdElephant(String idElephant) {
        IdElephant = idElephant;
    }

    public void setDaily_Log_Date(String daily_Log_Date) {
        Daily_Log_Date = daily_Log_Date;
    }

    public void setF_latitude(long f_latitude) {
        this.f_latitude = f_latitude;
    }

    public void setF_longitude(long f_longitude) {
        this.f_longitude = f_longitude;
    }

    public  Data_Model (){
    }
}

Firebase Database

and my log error

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.newapp.elephantapplication, PID: 31757
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Double to type com.newapp.elephantapplication.Data_Model
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:436)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
    at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
    at com.newapp.elephantapplication.LiveLocationActivity$1.onDataChange(LiveLocationActivity.java:58)
    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:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7224)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

It's my first time using Firebase; I've seen other questions on the subject, but I'm having trouble figuring out how to get it to work; I'm hoping you can assist me.

CodePudding user response:

As far as I can tell, your GPS node has three child nodes. One is the GPSData node with two properties under it, and then two nodes (f_latitude and f_longitude) with numeric values.

Since you're reading GPS and then looping over its children with for(DataSnapshot dataSnapshot : snapshot.getChildren()), the dataSnapshot will point to each of these three child nodes (GPSData, f_latitude and f_longitude) in turn. And while the values under GPSData match with your Data_Model class, the other two nodes are simple numeric values so can't be converted to a Data_Model object. That's what the error message tells you.

To make the current code work, move the f_latitude and f_longitude under the GPSData node.

  • Related