Skip to content
Advertisement

Data retrieve on Data Change in firebase getting Error

I am making an android app based on firebase. My logcat showing error on data change. Here is the code:

myRef.child("first").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
                    model model = childSnapshot.getValue(model.class);
                    if (dataSnapshot.getValue() == null) {
                       Toast.makeText(getApplicationContext(),"Sorry",Toast.LENGTH_SHORT).show();
                    } else {

                        String name = model.getFb01name();
                        

                        tvName.setText(name);
                        

                    }

                }
            }

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

            }


        });

Can anybody improve it?

Here is my logcat error message: It is showing error on this line.

 model model = childSnapshot.getValue(model.class);

“at my_project_name.SheetActivity$3.onDataChange(SheetActivity.java:156)”

2020-11-23 17:41:35.403 5585-5585/my_project_name E/AndroidRuntime: FATAL EXCEPTION: main
    Process: my_project_name, PID: 5585
    com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type my_project_name.Model.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 my_project_name.SheetActivity$3.onDataChange(SheetActivity.java:156)
        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:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

I have tried a lot. Sometimes it worked but stopped again. I cannot point out where is the mistake.

my model class goes as follow:

public class model {
    private String fb01name;
   


    public model() {
    }

    public model(String fb01name) {
        this.fb01name = fb01name;
        
    }

    public String getFb01name() {
        return fb01name;
    }

    public void setFb01name(String fb01name) {
        this.fb01name = fb01name;
    }

}

My firebase structure is like this:

this is my data structure

This the latest logcat message after applying the suggestion:

2020-11-24 00:40:06.783 12399-12399/my_project_nameE/AndroidRuntime: FATAL EXCEPTION: main
    Process: my_project_name, PID: 12399
    com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertString(CustomClassMapper.java:426)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:217)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(CustomClassMapper.java:179)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(CustomClassMapper.java:48)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:593)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:563)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:433)
        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 my_project_name.SheetActivity$3.onDataChange(SheetActivity.java:155)
        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:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Advertisement

Answer

Here try this, if your model class has only one parameter this should be fine. If you need all the parameters that are in your database, you will need to extract all from database and then add it to your model.

myRef.child("first").addValueEventListener(new ValueEventListener() {

    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
      String name = (String) dataSnapshot.child("fb01name").getValue().toString;
        if(name == null){
        
            Toast.makeText(getApplicationContext(),"Sorry",Toast.LENGTH_SHORT).show();
            return;
        
        }
    
        model model = new model(name);
        tvName.setText(name);
    
    
    }
    
    @Override
    public void onCancelled(@NonNull DatabaseError error) {
    
    }

});
Advertisement