Skip to content
Advertisement

I got the following error while pulling data from android studio Firebase

I could not understand the problem. It gave such an error when trying to pull the information from the Realtime Database or code.firebasteki to the screen, what is the problem?

Book

public class Book {
private String isim;
private String soyad;
private String numara;

public Book() {
}

public Book(String isim, String soyad, String numara) {
    this.isim = isim;
    this.soyad = soyad;
    this.numara = numara;
}

public String getIsim() {
    return isim;
}

public void setIsim(String isim) {
    this.isim = isim;
}

public String getSoyad() {
    return soyad;
}

public void setSoyad(String soyad) {
    this.soyad = soyad;
}

public String getNumara() {
    return numara;
}

public void setNumara(String numara) {
    this.numara = numara;
}

MainActivity

public class MainActivity extends AppCompatActivity {
private RecyclerView mrecyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mrecyclerView=(RecyclerView) findViewById(R.id.recyclerview_books);
    new FirebaseDatabaseHelper().readBooks(new FirebaseDatabaseHelper.DataStatus() {
        @Override
        public void DataIsLoaded(List<Book> books, List<String> keys) {
            new RecyclerView_Config().setConfig(mrecyclerView,MainActivity.this,books,keys);
        }

        @Override
        public void DataIsInserted() {

        }

        @Override
        public void DataIsUpdated() {

        }

        @Override
        public void DataIsDeleted() {

        }
    });
}

RecyclerView_Config

public class RecyclerView_Config {
private Context mContext;
private BookAdapter mbookAdapter;
public void setConfig(RecyclerView recyclerView,Context context,List<Book> books,List<String> 
 keys){
    mContext=context;
    mbookAdapter=new BookAdapter(books,keys);
    recyclerView.setLayoutManager(new LinearLayoutManager(context));
    recyclerView.setAdapter(mbookAdapter);
}


  class BookItemView extends RecyclerView.ViewHolder{
    private TextView misim;
    private TextView msoyad;
    private TextView mnumara;
    private String key;

    public BookItemView(ViewGroup parent){
        super(LayoutInflater.from(mContext).
                inflate(R.layout.book_list_item,parent,false));

        misim=(TextView) itemView.findViewById(R.id.isim_textView);
        msoyad=(TextView) itemView.findViewById(R.id.soyad_textView);
        mnumara=(TextView) itemView.findViewById(R.id.numara_textView);


    }
    public void bind(Book book,String key){
        misim.setText(book.getIsim());
        msoyad.setText(book.getSoyad());
        mnumara.setText(book.getNumara());
        this.key=key;
    }
}
class BookAdapter extends RecyclerView.Adapter<BookItemView>{
private List<Book> mbookList;
private List<String> mkeys;

    public BookAdapter(List<Book> mbookList, List<String> mkeys) {
        this.mbookList = mbookList;
        this.mkeys = mkeys;
    }

    @NonNull
    @Override
    public BookItemView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new BookItemView(parent);
    }

    @Override
    public void onBindViewHolder(@NonNull BookItemView holder, int position) {
        holder.bind(mbookList.get(position),mkeys.get(position));
    }

    @Override
    public int getItemCount() {
        return mbookList.size();
    }
}
 }

FirebaseDatabaseHelper

public class FirebaseDatabaseHelper {
private FirebaseDatabase mDatabase;
private DatabaseReference mReferenceBook;
private List<Book> books=new ArrayList<>();

public interface DataStatus{
    void DataIsLoaded(List<Book> books,List<String> keys);
    void DataIsInserted();
    void DataIsUpdated();
    void DataIsDeleted();
}

public FirebaseDatabaseHelper() {
    mDatabase=FirebaseDatabase.getInstance();
    mReferenceBook=mDatabase.getReference("Kullanicilar");
}
public void readBooks(final DataStatus dataStatus){
    mReferenceBook.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            books.clear();
            List<String> keys=new ArrayList<>();
            for(DataSnapshot keyNode:snapshot.getChildren()){
                keys.add(keyNode.getKey());
                Book book=keyNode.getValue(Book.class);
                books.add(book);
            }
            dataStatus.DataIsLoaded(books,keys);
        }

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

        }
    });
    }
   

firebase

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.phonebook, PID: 14998 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 com.example.phonebook.FirebaseDatabaseHelper$1.onDataChange(FirebaseDatabaseHelper.java:38) 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:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Advertisement

Answer

The field called numara is present in your Book class as a String while inside your database holds a number. That’s the reason why you get that error. To solve this, simply change the declaration of your class like this:

public class Book {
    private String isim;
    private String soyad;
           //πŸ‘‡
    private long numara;

    public Book() {
    }

                                          //πŸ‘‡
    public Book(String isim, String soyad, long numara) {
        this.isim = isim;
        this.soyad = soyad;
        this.numara = numara;
    }

    public String getIsim() {
        return isim;
    }

    public void setIsim(String isim) {
        this.isim = isim;
    }

    public String getSoyad() {
        return soyad;
    }

    public void setSoyad(String soyad) {
        this.soyad = soyad;
    }

          //πŸ‘‡
    public long getNumara() {
        return numara;
    }

                         //πŸ‘‡
    public void setNumara(long numara) {
        this.numara = numara;
    }
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement