java.lang.ClassCastException: java.util.HashMap cannot be cast to com.foodonomic.foodonomic.CategoryCode
My code gives this error when I use contacts.getData() to get the list, where contacts contacts is my BackendlessCollection received from server
java.lang.ClassCastException: java.util.HashMap cannot be cast to com.foodonomic.foodonomic.Category
Add this line of code before you make any data retrieval requests:
Backendless.Data.mapTableToClass( “CategoryCode”, CategoryCode.class );
Sorry but its not working.
Data is received in the BackendlessCollection and it shows as well when I use toString() on the received BackendlessCollection object and List<CategoryCode> code=foundContacts.getData(); this also doesn’t give any error and works fine but when I use some method for example code.toString() then it again gives me ClassCasteException. Here also if I write Log.d(“key”,code+""); it works, but writing Log.d(“key”,code.toString()) give an error.
My table and its name matches exactly with Class that I have used. If I try to save some object it gets saved to the same table only but that time also handleFault() is called where BackendlessFault is null.
Backendless.Data.mapTableToClass( "CategoryCode", CategoryCode.class );
Backendless.Persistence.of(CategoryCode.class).find(new AsyncCallback<BackendlessCollection<CategoryCode>>() {
@Override
public void handleResponse(BackendlessCollection<CategoryCode> foundContacts) {
Log.d("vivz","Inside handle response");
List<CategoryCode> code=foundContacts.getData();
Log.d("vivz",code.get(1).toString()); //Here I get That ClassCastException
}
@Override
public void handleFault(BackendlessFault fault) {
//Snackbar.make(coordinatorLayout,"Network error, check your internet connection",Snackbar.LENGTH_LONG).show();
Log.d("vivz", "Error");
}
});
Do you have a stack trace for the exception?
04-15 03:21:46.450 1878-1878/com.foodonomic.foodonomic E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.foodonomic.foodonomic, PID: 1878
java.lang.ClassCastException: java.util.HashMap cannot be cast to com.foodonomic.foodonomic.CategoryCode
at com.foodonomic.foodonomic.Foodonomic$CustomFragmentPagerAdapter$1.handleResponse(Foodonomic.java:207)
at com.foodonomic.foodonomic.Foodonomic$CustomFragmentPagerAdapter$1.handleResponse(Foodonomic.java:200)
at com.backendless.Persistence$10.handleResponse(Persistence.java:629)
at com.backendless.Persistence$10.handleResponse(Persistence.java:621)
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:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5323)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
What does the CategoryCode class look like?
package com.foodonomic.foodonomic;
import java.util.Date;
import java.util.HashMap;
/**
* Created by .hp on 14-04-2016.
*/
public class CategoryCode {
private String objectId;
private String categoryName;
private int categoryID;
CategoryCode(){
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public int getCategoryID() {
return categoryID;
}
public void setCategoryID(int categoryID) {
this.categoryID = categoryID;
}
public String getObjectId() {
return objectId;
}
public void setObjectId(String objectId) {
this.objectId = objectId;
}
}
Make the constructor public.
Thnku very much Mark… I feel So dumb now, such a silly mistake i did in my code. Thanks for giving me your valuable time.
You are really very cooperative. I feel so blessed that I have people like you to help me.
Thanks once again thanks aLLLLLLLOOOT …
Hello, Mark! Here is the same problem and i cant solve it. (5 days ago i didnt have it, when have made a service)
So, i use the service (Java/Android)
This is my code:
public BackendlessCollection<News> getNews(){
return Backendless.Persistence.of( News.class).find();
}
This code Backendless has generated :
public void getNewsAsync(AsyncCallback<BackendlessCollection<News>> callback)
{
Object[] args = new Object[]{};
Backendless.CustomService.invoke( SERVICE_NAME, SERVICE_VERSION_NAME, "getNews", args, com.backendless.BackendlessCollection.class, callback);
}
I have Activity. I have already write this
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NewsService.initApplication(this);
Backendless.Data.mapTableToClass("News", News.class);
newsService = NewsService.getInstance();
makeContext();
}
The method where i use my service method
And here is the problem
void makeContext(){
newsService.getNewsAsync(new AsyncCallback<BackendlessCollection<News>>() {
@Override
public void handleResponse(BackendlessCollection<News> newsBackendlessCollection) {
Log.e("news", "d " + newsBackendlessCollection.getCurrentPage().get(0).getTitle()
.toString()); // HERE IS THE PROBLEM
Log.e("news", "d " + newsBackendlessCollection.getCurrentPage().get(0)); //HERE ALL IS GOOD
...
My News.class is generated by Backendless. I have added the constructor
public class News {
private java.lang.String created;
private java.lang.String dateNews;
private int id;
private int idCategory;
private int likes;
private java.lang.String objectId;
private java.lang.String text;
private java.lang.String title;
public News(){}
public void setCreated(java.lang.String created) {
this.created = created;
}
public void setDateNews(java.lang.String dateNews) { this.dateNews = dateNews;}
public void setId(int id) {
this.id = id;
}
public void setIdCategory(int idCategory) {
this.idCategory = idCategory;
}
public void setLikes(int likes) {
this.likes = likes;
}
public void setObjectId(java.lang.String objectId) {
this.objectId = objectId;
}
public void setText(java.lang.String text) {
this.text = text;
}
public void setTitle(java.lang.String title) {
this.title = title;
}
public java.lang.String getCreated() {
return created;
}
public java.lang.String getDateNews() {
return dateNews;
}
public int getId() {
return id;
}
public int getIdCategory() {
return idCategory;
}
public int getLikes() {
return likes;
}
public java.lang.String getObjectId() {
return objectId;
}
public java.lang.String getText() {
return text;
}
public java.lang.String getTitle() {
return title;
}
}
Here is the stack trace
05-04 20:37:04.019 17166-17166/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.ClassCastException: java.util.HashMap cannot be cast to com.bommi.inews.News
at com.android.app.valeria.inews.MainActivity$2.handleResponse(MainActivity.java:93)
at com.android.app.valeria.inews.MainActivity$2.handleResponse(MainActivity.java:89)
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:95)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5283)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
My packages tree
http://support.backendless.com/public/attachments/53a21f8fffc82180c6c03ed8c8620950.png</img>
Also i have updated backendless.jar
Hope i have tried all to solve the problem…
Even dont know which way i should think to solve the problem…
Regards,
Valeria
Please let me know your app id.