1 votes

Can't convert object of type java.lang.String to type

I have an app that brings RealTime data from firebase but I get an error that does not give me with more data, I make the call to the data with the first databasereference and get that data to add them to a list that data works fine but in the second databasereference that is inside onDataChange of the first databasereference gives me the following error:

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.smbr_v21.model.urls.UrlUser
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@16.1.0:423)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@16.1.0:214)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@16.1.0:79)
    at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@16.1.0:212)
    at com.example.smbr_v21.view.activitys.NoticesActivity$6$1.onDataChange(NoticesActivity.java:288)
    at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.1.0:75)
    at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.1.0:63)
    at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.1.0:55)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6144)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

But I don't understand why I get this error, I already checked the variables if it could be that they don't match but all are ok, this is my code that brings the data from RealTime:

emailNode = firebaseUser.getEmail().replace(".", "-");
    emailNodeNew = emailNode.replace("@", "_");
    Log.i(Constants.TAG_NOTICES_ACTIVITY, "user >> " + firebaseUser.getEmail() +
            "\nuserMod >> " + emailNodeNew);

    dialog.setMessage(Constants.MESSAGE_DIALOG);
    dialog.show();

    databaseReference.child(Constants.DATES_NODE_POST)
            .addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if(dataSnapshot.exists()){
                        for(DataSnapshot dsd : dataSnapshot.getChildren()){
                            DatesNotices datesNotices = dsd.getValue(DatesNotices.class);

                            datesGet.add(datesNotices.getNickname());
                            datesGet.add(datesNotices.getTitle());
                            datesGet.add(datesNotices.getDate());
                            datesGet.add(datesNotices.getText());

                            databaseReference.child(Constants.DATES_NODE)
                                    .child(Constants.DATES_NODE_URLS)
                                    .child(emailNodeNew)
                                    .child("urlUser")
                                    .addValueEventListener(new ValueEventListener() {
                                        @Override
                                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                            if(dataSnapshot.exists()){
                                                linearNoPosts.setVisibility(View.GONE);
                                                Log.i(Constants.TAG_NOTICES_ACTIVITY, "Exist");
                                                for (DataSnapshot ds : dataSnapshot.getChildren()){
                                                    UrlUser urlUser = ds.getValue(UrlUser.class); //Línea del error
                                                    urlPerfil = urlUser.getUrlUserPerfil();
                                                    Log.w(Constants.TAG_NOTICES_ACTIVITY, urlUser.getUrlUserPerfil());

                                                    addObjects(datesGet, urlPerfil);
                                                }
                                            } else{
                                                Log.i(Constants.TAG_NOTICES_ACTIVITY, "No-Exist");
                                                deleteNoticesRefresh();
                                                linearNoPosts.setVisibility(View.VISIBLE);
                                            }

                                            dialog.dismiss();
                                        }

                                        @Override
                                        public void onCancelled(@NonNull DatabaseError databaseError) {
                                            dialog.dismiss();
                                            Log.w(Constants.TAG_NOTICES_ACTIVITY, "Error al traer los datos:\n" +
                                                    databaseError);
                                            Toast.makeText(NoticesActivity.this,
                                                    "Hubo un error al cargar las publicaciones",
                                                    Toast.LENGTH_SHORT).show();
                                        }
                                    });
                        }
                    } else{
                        Log.i(Constants.TAG_NOTICES_ACTIVITY, "No-Exist");
                        deleteNoticesRefresh();
                        linearNoPosts.setVisibility(View.VISIBLE);
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    dialog.dismiss();
                    Log.w(Constants.TAG_NOTICES_ACTIVITY, "Error al traer los datos:\n" +
                            databaseError);
                    Toast.makeText(NoticesActivity.this,
                            "Hubo un error al cargar las publicaciones",
                            Toast.LENGTH_SHORT).show();
                }
            });
}

This is my UrlUser.class model:

public class UrlUser {

    private String urlUserPerfil;

    public UrlUser() {
    }

    public UrlUser(String urlUserPerfil) {
        this.urlUserPerfil = urlUserPerfil;
    }

    public String getUrlUserPerfil() {
        return urlUserPerfil;
    }

    public void setUrlUserPerfil(String urlUserPerfil) {
        this.urlUserPerfil = urlUserPerfil;
    }
}

I have checked firebase but I don't see any errors either:

introducir la descripción de la imagen aquí

Thank you in advance.

1voto

Cadeq Points 378

As the error says, you cannot convert a String to another object, even if it contains a String parameter.

This is how it fails

    UrlUser urlUser = ds.getValue(UrlUser.class);

Change it like this

UrlUser urlUser = new UrlUser(ds.getValue(String.class))

Since you have an UrlUser constructor that accepts a String, use it.


I don't know exactly how the DataSnapshot but if you can get the String, you won't get complicated with parses between objects.

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X