In some custom event I put an object into map, but when I get this map as a response I get the following error:
Exception in thread "pool-1-thread-1" java.lang.ClassCastException: java.util.HashMap cannot be cast to com.list.models.Aua
This line causes it:
Aua aua = (Aua) result.get("last");
However, if I just see what response map contains, I am able to see some object with needed properties
I have a public constructor for this object.
Why do the casting causes error? Thank you
Try to add the following line before any calls to table ‘Aua’:
Backendless.Data.mapTableToClass('Aua', Aua.class);
If it doesn’t help - provide your Aua class here.
public class Test {
private String objectId;
private java.util.Date updated;
private String testProperty;
private java.util.Date created;
private String ownerId;
public String getObjectId()
{
return this.objectId;
}
public java.util.Date getUpdated()
{
return this.updated;
}
public String getTestProperty()
{
return this.testProperty;
}
public java.util.Date getCreated()
{
return this.created;
}
public String getOwnerId()
{
return this.ownerId;
}
public void setObjectId( String objectId )
{
this.objectId = objectId;
}
public void setUpdated( java.util.Date updated )
{
this.updated = updated;
}
public void setTestProperty( String testProperty )
{
this.testProperty = testProperty;
}
public void setCreated( java.util.Date created )
{
this.created = created;
}
public void setOwnerId( String ownerId )
{
this.ownerId = ownerId;
}
public Test save()
{
return Backendless.Data.of( Test.class ).save( this );
}
public Long remove()
{
return Backendless.Data.of( Test.class ).remove( this );
}
public static Test findById( String id )
{
return Backendless.Data.of( Test.class ).findById( id );
}
public static Test findFirst()
{
return Backendless.Data.of( Test.class ).findFirst();
}
public static Test findLast()
{
return Backendless.Data.of( Test.class ).findLast();
}
public Test(){}
}
Here is my business logic function:
List<Test> list = null;
HashMap<String, Object> result = new HashMap<String, Object>();
@Override
public Map handleEvent( RunnerContext context, Map eventArgs )
{
Backendless.Data.mapTableToClass("Test", Test.class);
BackendlessCollection<Test> response = Backendless.Persistence.of( Test.class ).find();
list = response.getData();
Test test = list.get(list.size() - 1);
result.put("last", test);
return result;
}
Here is how I get my object:
Test test = (Test) result.get("last");
System.out.println(test.getTestProperty());
my Test table has only 1 property (testProperty)
Where does “Aua” come from then?
Sorry, I forgot to mention that I made a test class for this exact test, because that Aua class had too much properties. Both of them thow cast exception
Does the class belong to a package?
yes. I thought I was missing something in a class defenition.
Try to change
HashMap<String, Object> result = new HashMap<String, Object>();
to
HashMap<String, Test> result = new HashMap<String, Test>();
in your business logic code.
Just to make sure I understand: you have a custom event which returns the Test object in that hashmap. Then you invoke the custom event from the client app and that’s when the error occurs?
@AleksandrNavara Unfortunately, it did not help
@MarkPiller Yes, it is absolutely correct
Did you replicate the Test class on the client side the same way? (in other words, are they identical between server-code and client-code?)
Do you have the Backendless.Data.mapTableToClass call on the client-side?
Thank you a lot, it worked!