HashMap cannot be cast to (Moving from Parse)

Hi

I’m having trouble, by the looks of it mapping my class to my database table.

Here is my class file, my manager class and response:-

public class Food {

private String ownerId;
private String objectId;
private java.util.Date updated;
private java.util.Date created;
private BackendlessUser owner;
private String name;
private String measurement;
private double carbG;
private int energyKcal;
private double fatMonoG;
private double fatPolyG;
private double fatSatG;
private double fibreG;
private double ironMg;
private double magnesiumMG;
private double potassiumMG;
private double proteinG;
private double riboflavinMG;
private double sodiumMG;
private double sugarG;
private double waterG;
private double weight;
private double zincMG;
private double calciumMG;

public Food(String ownerId, String objectId, Date updated, Date created, BackendlessUser owner, String name, String measurement, double carbG, int energyKcal, double fatMonoG, double fatPolyG, double fatSatG, double fibreG, double ironMg, double magnesiumMG, double potassiumMG, double proteinG, double riboflavinMG, double sodiumMG, double sugarG, double waterG, double weight, double zincMG, double calciumMG) {
    this.ownerId = ownerId;
    this.objectId = objectId;
    this.updated = updated;
    this.created = created;
    this.owner = owner;
    this.name = name;
    this.measurement = measurement;
    this.carbG = carbG;
    this.energyKcal = energyKcal;
    this.fatMonoG = fatMonoG;
    this.fatPolyG = fatPolyG;
    this.fatSatG = fatSatG;
    this.fibreG = fibreG;
    this.ironMg = ironMg;
    this.magnesiumMG = magnesiumMG;
    this.potassiumMG = potassiumMG;
    this.proteinG = proteinG;
    this.riboflavinMG = riboflavinMG;
    this.sodiumMG = sodiumMG;
    this.sugarG = sugarG;
    this.waterG = waterG;
    this.weight = weight;
    this.zincMG = zincMG;
    this.calciumMG = calciumMG;
}

public String getOwnerId() {
    return ownerId;
}

public void setOwnerId(String ownerId) {
    this.ownerId = ownerId;
}

public String getObjectId() {
    return objectId;
}

public void setObjectId(String objectId) {
    this.objectId = objectId;
}

public Date getUpdated() {
    return updated;
}

public void setUpdated(Date updated) {
    this.updated = updated;
}

public Date getCreated() {
    return created;
}

public void setCreated(Date created) {
    this.created = created;
}

public BackendlessUser getOwner() {
    return owner;
}

public void setOwner(BackendlessUser owner) {
    this.owner = owner;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getMeasurement() {
    return measurement;
}

public void setMeasurement(String measurement) {
    this.measurement = measurement;
}

public double getCarbG() {
    return carbG;
}

public void setCarbG(double carbG) {
    this.carbG = carbG;
}

public int getEnergyKcal() {
    return energyKcal;
}

public void setEnergyKcal(int energyKcal) {
    this.energyKcal = energyKcal;
}

public double getFatMonoG() {
    return fatMonoG;
}

public void setFatMonoG(double fatMonoG) {
    this.fatMonoG = fatMonoG;
}

public double getFatPolyG() {
    return fatPolyG;
}

public void setFatPolyG(double fatPolyG) {
    this.fatPolyG = fatPolyG;
}

public double getFatSatG() {
    return fatSatG;
}

public void setFatSatG(double fatSatG) {
    this.fatSatG = fatSatG;
}

public double getFibreG() {
    return fibreG;
}

public void setFibreG(double fibreG) {
    this.fibreG = fibreG;
}

public double getIronMg() {
    return ironMg;
}

public void setIronMg(double ironMg) {
    this.ironMg = ironMg;
}

public double getMagnesiumMG() {
    return magnesiumMG;
}

public void setMagnesiumMG(double magnesiumMG) {
    this.magnesiumMG = magnesiumMG;
}

public double getPotassiumMG() {
    return potassiumMG;
}

public void setPotassiumMG(double potassiumMG) {
    this.potassiumMG = potassiumMG;
}

public double getProteinG() {
    return proteinG;
}

public void setProteinG(double proteinG) {
    this.proteinG = proteinG;
}

public double getRiboflavinMG() {
    return riboflavinMG;
}

public void setRiboflavinMG(double riboflavinMG) {
    this.riboflavinMG = riboflavinMG;
}

public double getSodiumMG() {
    return sodiumMG;
}

public void setSodiumMG(double sodiumMG) {
    this.sodiumMG = sodiumMG;
}

public double getSugarG() {
    return sugarG;
}

public void setSugarG(double sugarG) {
    this.sugarG = sugarG;
}

public double getWaterG() {
    return waterG;
}

public void setWaterG(double waterG) {
    this.waterG = waterG;
}

public double getWeight() {
    return weight;
}

public void setWeight(double weight) {
    this.weight = weight;
}

public double getZincMG() {
    return zincMG;
}

public void setZincMG(double zincMG) {
    this.zincMG = zincMG;
}

public double getCalciumMG() {
    return calciumMG;
}

public void setCalciumMG(double calciumMG) {
    this.calciumMG = calciumMG;
}

This is my code:-

public class FoodManager {
private static final IDataStore<Food> DATA_STORE = Backendless.Persistence.of(Food.class);
private static final BackendlessDataQuery backendlessDataQuery = new BackendlessDataQuery();

static {
    //backendlessDataQuery.setWhereClause(DEFAULT_WHERE_CLAUSE);
    backendlessDataQuery.setQueryOptions(new QueryOptions(50, 0));
}

public static void remove(final Food entity, Context context, final InnerCallback&lt;Long&gt; callback) {
    DATA_STORE.remove(entity, new DefaultCallback&lt;Long&gt;(context) {
        @Override
        public void handleResponse(Long response) {
            super.handleResponse(response);

            if (callback != null)
                callback.handleResponse(response);
        }
    });
}

public static void saveEntity(final Food entity, Context context, final InnerCallback&lt;Food&gt; callback) {
    DATA_STORE.save(entity, new DefaultCallback&lt;Food&gt;(context) {
        @Override
        public void handleResponse(Food response) {
            super.handleResponse(response);

            if (callback != null)
                callback.handleResponse(response);
        }
    });
}

public static void findEntities(String query, final AsyncCallback&lt;List&lt;Food&gt;> callback) {
    backendlessDataQuery.setWhereClause("name LIKE '" + query + "%'");
    FoodManager.DATA_STORE.find(backendlessDataQuery, new AsyncCallback&lt;BackendlessCollection&lt;Food&gt;>() {
                @Override
                public void handleResponse(BackendlessCollection&lt;Food&gt; response) {
                    
                    Log.i("MyApp", response.getCurrentPage().get(0).getName());
                    
                    callback.handleResponse(response.getCurrentPage());
                }

                @Override
                public void handleFault(BackendlessFault fault) {
                    callback.handleFault(fault);
                }
            }

    );
}This is where I call it:-

mFoodSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
FoodManager.findEntities(query, new AsyncCallback<List<Food>>() {
@Override
public void handleResponse(List<Food> response) {
if (response.size() > 1) {
foodList = response;
mResultsAdapter = new FoodSearchResultsRecyclerAdapter(foodList);
mResultsRecyclerView.setAdapter(mResultsAdapter);
}
}
This issue I’m having is that the response is correct, it’s returning a list of 30 items.
Log.i(“MyApp”, response.getCurrentPage().get(0).getName());I get this error:-FATAL EXCEPTION: main
Process: com.purewowstudio.bodycal, PID: 31929
java.lang.ClassCastException: java.util.HashMap cannot be cast to com.purewowstudio.bodycal.models.Food
at com.purewowstudio.bodycal.tasks.FoodManager$3.handleResponse(FoodManager.java:58)
at com.purewowstudio.bodycal.tasks.FoodManager$3.handleResponse(FoodManager.java:54)
at com.backendless.Persistence$10.handleResponse(Persistence.java:615)
at com.backendless.Persistence$10.handleResponse(Persistence.java:607)
at com.backendless.async.message.AsyncMessage$ResponseHandler.handle(AsyncMessage.java:64)
at com.backendless.async.message.AsyncMessage.handleCallback(AsyncMessage.java:41)
at com.backendless.core.AndroidCarrier$1.handleMessage(AndroidCarrier.java:37)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Please can anyone help or point me in the right direction. The class and table names match. The schema matches the pojo. Not sure what the issue is

It is not clear from your message where the error occurs. Could you narrow it down and point out the code which results in the error?

Also, reusing BackendlessDataQuery (keeping it a singleton) is not a good idea. Create a new object whenever you need it.

Sorry Mark, I was reusing the examples available and appreciate there’s a lot of code above. The problem occurs on this line:-

Log.i(“MyApp”, response.getCurrentPage().get(0).getName());

It looks as though the search is returning results, but when I try to access data from the servers returned results it’s causing an issue. Hashmap cannot be cast to…

I have read on other answers you have gave people that the database isn’t mapping correctly to the model I have created. I have looked at my model and it seems very similar to the database table. Both named “Food” as well.

I really appreciate the help.

No worries, I’ve fixed it. My mistake. I have had a standard constructor in the model I was using. I noticed it from your examples.

My model now consists of variables with getters and setters only.

BTW really appreciate the help. What a great service you have! Thank you for your help.

Hi Phil,

You can use either JavaBean style getters/setters and/or public fields. Default no-arg constructor is required on the data objects used in data transfer between the client and the server.

Regards,
Mark