Skip to content Skip to sidebar Skip to footer

Cant Convert Object Of Type Java.util.arraylist Read From Firebase Realtim Database Nested Objects

This Firebase database structure: enter image description here Here is my Device Class: public class Device { private String title; private Map CounterN

Solution 1:

You are getting the following error:

can't convert object of type java.util.ArrayList to type com.example.smartappfirebase.Counters

Because you getting an ArrayList object and not a Counters object and there is no way in Java to create such a cast. Your counters node that exists within the cRVh ... NHeT node is actually an array. When you try to read that array in code, it is read as an ArrayList, hence that error. As I see in your screenshot, that array contains Counters objects. Unfortunately, you cannot simply map that array of Counters objects to an ArrayList<Counters>. The counters node is actually an ArrayList of HashMaps. So if you need to have an ArrayList<Counters>, then you should iterate and create that List yourself. Besides that, the name of the properties in your Counters class do not match the names in the database. See, countername vs name and counternumber vs counter. All properties must match.

Solution 2:

You should not try to cast HashMap to ArrayList. Though, you may try to get keys() or values() of the HashMap.

And you should post full exception log, not as image.

Solution 3:

I try to change

 Arraylist
package com.example.smartappfirebase;

import java.util.ArrayList;

public class Device{

privateString title;
privateArrayList<counters> counters;

publicArrayList<counters> getCounter() {
    return counters;
}

publicvoidsetCounter(ArrayList<counters> counters) {
    this.counters = counters;
}

publicDevice(){}

publicDevice(String title,ArrayList<counters> counters) {
    this.title = title;
    this.counters=counters;
}

publicStringgetTitle() {
    return title;
}

publicvoidsetTitle(String title) {
    this.title = title;
}

}

Result is:

  java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference 

Post a Comment for "Cant Convert Object Of Type Java.util.arraylist Read From Firebase Realtim Database Nested Objects"