Error: HashMap cannot be cast to class of android studio

I made a table named GKBACK which consists of attributes like GK ques,option,answer etc. I need to fetch an answer from the table and display it in textview in android studio.
Error: HashMap cannot be cast to com.example.akhil.quizit.GKBACK
The following code i have implemented in android studio:

GKPaper.class
BackendlessDataQuery dataQuery = new BackendlessDataQuery();
dataQuery.setWhereClause(“ID=2”);
Backendless.Data.of( GKBACK.class ).find(dataQuery, new AsyncCallback<BackendlessCollection<GKBACK>>() {
@Override
public void handleResponse(BackendlessCollection<GKBACK> nycPeople) {
Iterator<GKBACK> iterator = nycPeople.getCurrentPage().iterator();
while (iterator.hasNext()) {
GKBACK person = iterator.next();
t.setText(person.ANS);
}
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
System.out.println("Server reported an error - " + backendlessFault.getMessage());
}
});
}
GKBACK Class
public class GKBACK {
private Date created;
private Date updated;
public int ID;
public String ANS;
public String OPTION1;
public String OPTION2;
public String OPTION3;
public String OPTION4;
public String QUES;
public String getANS() {
return ANS;
}
public void setANS(String ANS) {
this.ANS = ANS;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public Date getCreated()
{
return created;
}
public void setCreated( Date created )
{
this.created = created;
}
public void setQUES(String QUES) {
this.QUES = QUES;
}
public String getQUES() {
return QUES;
}
public void setOPTION4(String OPTION4) {
this.OPTION4 = OPTION4;
}
public String getOPTION4() {
return OPTION4;
}
public void setOPTION3(String OPTION3) {
this.OPTION3 = OPTION3;
}
public String getOPTION3() {
return OPTION3;
}
public void setOPTION2(String OPTION2) {
this.OPTION2 = OPTION2;
}
public String getOPTION2() {
return OPTION2;
}
public void setOPTION1(String OPTION1) {
this.OPTION1 = OPTION1;
}
public String getOPTION1() {
return OPTION1;
}
public Date getUpdated()
{
return updated;
}
public void setUpdated( Date updated )
{
this.updated = updated;
}
}
I tried to solve this problem by using:
Backendless.Data.mapTableToClass( “GKBACK”, GKBACK.class );
But it didn’t help me…

Hi Akhil,

Everything looks good here. Could you please tell me which line throws the exception? Is it here:?
GKBACK person = iterator.next();

Regards,
Mark

2 lines generating errors:
first
Backendless.Data.of( GKBACK.class ).find(dataQuery, new AsyncCallback<BackendlessCollection<GKBACK>>() {

and
Second

GKBACK person = iterator.next();

Hi Akhil,

Could you please let me know if you’re compiling against backendless.jar from maven or you downloaded it from our website?

Regards,
Mark

I downloaded backendless.jar from your site only. There is no error message where i initialized backlendless. I initialized it in the main activity and accesing it in another activity. Is this causing an error??

Hi Akhil,

Here’s what I have done:

    I created GKBACK table in my app: http://support.backendless.com/public/attachments/01b95ffa67b28a9684addb2d9a18271b.png</img> I added two records to the table: http://support.backendless.com/public/attachments/7c103489799a1a5fc68df836ab62d48f.png</img> I ran the code which is identical to yours (please check if there are any differences):
    private void getGBBACK()
    {
        AsyncCallback&lt;BackendlessCollection&lt;GKBACK&gt;> findCallback = new AsyncCallback&lt;BackendlessCollection&lt;GKBACK&gt;>() {
            @Override
            public void handleResponse(BackendlessCollection&lt;GKBACK&gt; gkbackBackendlessCollection) {
                GKBACK gkBackObject = gkbackBackendlessCollection.getCurrentPage().get( 0 );
                Log.i( "MY APP", gkBackObject.ANS );
                Log.i( "MY APP", gkBackObject.OPTION1 );
                Log.i( "MY APP", gkBackObject.QUES );
            }


            @Override
            public void handleFault(BackendlessFault backendlessFault) {


            }
        };
        BackendlessDataQuery dataQuery = new BackendlessDataQuery();
        dataQuery.setWhereClause( "ID = 2");
        Backendless.Data.of( GKBACK.class ).find( dataQuery, findCallback );
    }

The end result - it runs just fine without any problems:
http://support.backendless.com/public/attachments/4b5ba60bd404429e698e404a146b2563.png&lt;/img&gt;

Here’s what you could try:

    Get the latest SDK and make sure to update your project with backendless.jar from it See if there are any differences between my table schema and yours. Send me your app id and secret key and I can run my code against your app.
Regards, Mark

Can you please send me the snapshot of GKBACK.java which you have created in android studio. I think i am making mistake in GKBACK.java. In that class i have set the getter and setter of all the columns in GKBACK which i have created in Backendless and nothing else.

I didn’t create a new class - I used the one you posted here in the very first post.

Then i think the prob might be that i changed the table name from gk to GKBACK so hashmaping to GKBACK is creating an error. Am i right??
the error message was also that cannot hashmap to com.quitit.GKBACK class

That’s an important detail!! )) Class name must match the table name. If they do not, you must do this:

Backendless.Data.mapTableToClass( TABLENAME, YourClass.class )

for example:

Backendless.Data.mapTableToClass( "gk", GKBACK.class )

Still my problem is not solved…
What to do…??? :frowning: :frowning:

Make sure the name of the table and the name of the class are identical.

Ya its same…

Impossible… tens of thousands of apps would break then…

may to send the code in android studio so that i can run it…

I came across the same issue. I tested my app in debug build and it was working fine. When I tried to do the same in a release build, I got this error. I’ve configured Proguard but still no luck. Below is my Proguard configuration:

Backendless

-dontwarn com.backendless.**
-dontwarn weborb.**
-keep class weborb.** {;}
-keep class com.backendless.** {
;}
-keep class com.qatth.qatth.Model.** {*;}

Is there any issue with the configuration?

Please show the code which results in error.

This is inside recyclerView adapter:

final Trip currentTrip = trips.get(position);
final cities city = currentTrip.getCity().get(0);Here is the Trip class:public class Trip
{
private String ownerId;
private java.util.Date created;
private Integer numberOfTravelers;
private java.util.Date updated;
private java.util.Date endDate;
private java.util.Date startDate;
private String currency;
private Integer totalBudget;
private String travellers;
private String objectId;
private java.util.List<cities> city;
private java.util.List<Payment> payment;…

Try adding the following code right after you call Backendless.initApp:

Backendless.Data.mapTableToClass( “cities”, cities.class );

Backendless.Data.mapTableToClass( “Payment”, Payment.class );

Many thanks. Problem solved!