Vivek Thakkar
Vivek Thakkar

Reputation: 135

W/ClassMapper: No setter/field for categoryName found on class

I'm trying to retrieve data from Firebase Database and display it in the views. The code works fine when the data in ArrayList is hardcoded. But it displays empty views when the data in ArrayList is filled with data retrieved from Firebase Database. This is what I'm getting in logs:

W/ClassMapper: No setter/field for categoryName found on class
W/ClassMapper: No setter/field for categoryImageUrl found on class
and so on..

Here is the MainActivity.java(ECartHomeActivity.java): (used for fetching data)

public class ECartHomeActivity extends AppCompatActivity {
    public static final String DATABASE_PATH_UPLOADS = "ProductCategoryModel";
    // firebase objects
    DatabaseReference databaseMessages;
    ArrayList<ProductCategoryModel> productCategoryList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ecart);
        productCategoryList = new ArrayList<ProductCategoryModel>();
        databaseMessages = FirebaseDatabase.getInstance().getReference(DATABASE_PATH_UPLOADS);
        databaseMessages.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                productCategoryList.clear();
                for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()){
                    // iterates through all the messages
                    ProductCategoryModel message = messageSnapshot.getValue(ProductCategoryModel.class);
                    ProductCategoryModel fire = new ProductCategoryModel();
                    String categoryName = message.getProductCategoryName();
                    String categoryDescription = message.getProductCategoryDescription();
                    String categoryDiscount = message.getProductCategoryDiscount();
                    String categoryImageUrl = message.getProductCategoryImageUrl();
                    fire.setProductCategoryName(categoryName);
                    fire.setProductCategoryDescription(categoryDescription);
                    fire.setProductCategoryDiscount(categoryDiscount);
                    fire.setProductCategoryImageUrl(categoryImageUrl);
                    productCategoryList.add(fire);
                }
                CenterRepository.getCenterRepository().setListOfCategory(productCategoryList);
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }

It shows data in the views if the "productCategoryList" ArrayList is hardcoded. But no data is displayed if this ArrayList is filled with data retrieved from Database.

ProductCategoryModel.java

public class ProductCategoryModel {
    public ProductCategoryModel(){
    }

    private String categoryName;
    private String categoryDescription;
    private String categoryDiscount;
    private String categoryImageUrl;
    /**
    * @param productCategoryName
    * @param productCategoryDescription
    * @param productCategoryDiscount
    * @param productCategoryUrl
    */
    public ProductCategoryModel(String productCategoryName, String productCategoryDescription,
    String productCategoryDiscount, String productCategoryUrl) {
        super();
        this.categoryName = productCategoryName;
        this.categoryDescription = productCategoryDescription;
        this.categoryDiscount = productCategoryDiscount;
        this.categoryImageUrl = productCategoryUrl;
    }

    /**
    * @return the idproductcategory
    */
    public String getProductCategoryName() {
        return categoryName;
    }

    /**
    * @param categoryName
    *            the idproductcategory to set
    */
    public void setProductCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    /**
    * @return the productDescription
    */
    public String getProductCategoryDescription() {
        return categoryDescription;
    }

    /**
    * @param categoryDescription
    *            the productDescription to set
    */
    public void setProductCategoryDescription(String categoryDescription) {
        this.categoryDescription = categoryDescription;
    }

    /**
    * @return the productDiscount
    */
    public String getProductCategoryDiscount() {
        return categoryDiscount;
    }

    /**
    * @param categoryDiscount
    *            the productDiscount to set
    */
    public void setProductCategoryDiscount(String categoryDiscount) {
        this.categoryDiscount = categoryDiscount;
    }

    /**
    * @return the productUrl
    */
    public String getProductCategoryImageUrl() {
        return categoryImageUrl;
    }

    /**
    * @param categoryImageUrl
    *            the productUrl to set
    */
    public void setProductCategoryImageUrl(String categoryImageUrl) {
        this.categoryImageUrl = categoryImageUrl;
    }

}

This is the JSON code:

{
    "ProductCategoryModel" : {
        "1" : {
            "categoryName" : "anonymous",
            "categoryDescription" : "Heyyyy",
            "categoryDiscount" : "anonymous",
            "categoryImageUrl" : "anonymous"
        }
        ,
        "2" : {
            "categoryName" : "anonymous",
            "categoryDescription" : "Heyyyy",
            "categoryDiscount" : "anonymous",
            "categoryImageUrl" : "anonymous"
        }
        ,
        "3" : {
            "categoryName" : "anonymous",
            "categoryDescription" : "Heyyyy",
            "categoryDiscount" : "anonymous",
            "categoryImageUrl" : "anonymous"
        }
    }

}

Upvotes: 0

Views: 2062

Answers (1)

TankRaj
TankRaj

Reputation: 89

Generate the getter and setter(and constructor) of your ProductCategoryModel class by android studio's default shortcut(alt+insert or right click and generate) since there may be mistakes on some letters while typing manually and you can replace

for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()){  // iterates through all the messages
            ProductCategoryModel message = messageSnapshot.getValue(ProductCategoryModel.class);
            ProductCategoryModel fire = new ProductCategoryModel();
            String categoryName = message.getProductCategoryName();
            String categoryDescription = message.getProductCategoryDescription();
            String categoryDiscount = message.getProductCategoryDiscount();
            String categoryImageUrl = message.getProductCategoryImageUrl();
            fire.setProductCategoryName(categoryName);
            fire.setProductCategoryDescription(categoryDescription);
            fire.setProductCategoryDiscount(categoryDiscount);
            fire.setProductCategoryImageUrl(categoryImageUrl);
            productCategoryList.add(fire);
        }

this on some smart way as:

for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()){  // iterates through all the messages

            String categoryName = message.getProductCategoryName();
            String categoryDescription = message.getProductCategoryDescription();
            String categoryDiscount = message.getProductCategoryDiscount();
            String categoryImageUrl = message.getProductCategoryImageUrl();
            ProductCategoryModel fire = new ProductCategoryModel(categoryName,categoryDescription,categoryDiscount,categoryImageUrl);
            productCategoryList.add(fire);
        }

there seems to be problem with your model class, i hope this may solve your problem.

Upvotes: 1

Related Questions