Search code examples
javaandroidhashmapclasscastexceptionbackendless

ClassCastException while using BackendlessCollection


java.lang.ClassCastException: java.util.HashMap cannot be cast to com.foodonomic.foodonomic.Category

I was working with Backendless.com APIs in one of my app. There when I Tried to call data from server

using this method

//Category is my class whose object I was trying to fetch from the Backendless //Server
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());


                }

                @Override
                public void handleFault(BackendlessFault fault) {
                    //Snackbar.make(coordinatorLayout,"Network error, check your internet connection",Snackbar.LENGTH_LONG).show();

                    Log.d("vivz", "Error");
                }
            });

I got a Runtime Exception java.lang.ClassCastException: java.util.HashMap cannot be cast to com.foodonomic.foodonomic.Category

I have asked the same question there on Backendless Support, keeping in mind to get answers from more users of Backendless I am posting this here. Here is the link to the post that I had posted there on Backendless

http://support.backendless.com/topic/java-lang-classcastexception-java-util-hashmap-cannot-be-cast-to-com-foodonomic-foodonomic-category#comment-13573


Solution

  • As I responded on the Backendless support forum, the solution was to declare the default constructor of the CategoryCode class public.