User to User relations? Friend list android example

Well, it was too early to celebrate, I have a weird problem.
This code for FRIENDS column works as it is intended, without bugs.

BackendlessUser user = Backendless.UserService.CurrentUser();
BackendlessUser[] friends = (BackendlessUser[]) user.getProperty(BackendlessUserProperties.FRIENDS);

ArrayList<BackendlessUser> friendList = new ArrayList<BackendlessUser>();
friendList.addAll(Arrays.asList(friends));
friendList.add(backendlessUserItem);

user.setProperty(BackendlessUserProperties.FRIENDS, friendList);
Backendless.UserService.update(user, new DefaultCallback<BackendlessUser>(context));


But if I try something like that but for other columns like REQUESTS or PENDINGS it gives me the error

java.lang.Object[] cannot be cast to java.util.List<com.backendless.BackendlessUser>


And this is the code that I used for this, same as above just changed from friends to pendings

BackendlessUser user = Backendless.UserService.CurrentUser();
BackendlessUser[] pendings = (BackendlessUser[]) user.getProperty(BackendlessUserProperties.PENDINGS);

ArrayList<BackendlessUser> pendingList = new ArrayList<BackendlessUser>();
pendingList.addAll(Arrays.asList(pendings));
pendingList.add(backendlessUserItem);

user.setProperty(BackendlessUserProperties.PENDINGS, pendingList);
Backendless.UserService.update(user, new DefaultCallback<BackendlessUser>(context));

So the problem is that it works for friends column but not for the others, even though each of them is related to Users just like friends.

Unfortunately this tell me nothing:

java.lang.Object[] cannot be cast to java.util.List<com.backendless.BackendlessUser>

Do you have a stack trace for the error?


2-22 21:35:25.582 13292-13292/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.thrillme.app, PID: 13292
                                                   java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.backendless.BackendlessUser[]   at com.thrillme.app.Adapters.FoundedUsersListViewAdapter$1.onClick(FoundedUsersListViewAdapter.java:121) at android.view.View.performClick(View.java:5204)
    at android.view.View$PerformClick.run(View.java:21156)
                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                       at android.os.Looper.loop(Looper.java:148)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5466)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Points to this line:

BackendlessUser[] pendings = (BackendlessUser[]) user.getProperty(BackendlessUserProperties.PENDINGS);

This is the error it gives


                                                   java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.backendless.BackendlessUser[]

Well, the stack trace points to a line in your code (line 121 in FoundedUsersListViewAdapter). That’s where the bad cast is going on:

com.thrillme.app.Adapters.FoundedUsersListViewAdapter$1.onClick(FoundedUsersListViewAdapter.java:121)

Yes, that’s this line:
BackendlessUser[] pendings = (BackendlessUser[]) user.getProperty(BackendlessUserProperties.PENDINGS);

What I don’t understand is why it works with FRIENDS column but not PENDINGS, same code otherwise.

Perhaps PENDINGS for that object is an empty collection/array? If that’s the case, change the code to check if the array contains anything, if it does, then cast to BackendlessUser[], otherwise, there is no data to work with.

Ok, thanks I will try to add the check for empty list.

I have been having the exact same problem for the past few days and waiting for your patch to fix it. Unfortunately this patch does not solve my issue.

I get this error message:

java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.backendless.BackendlessUser[]
at android.larrimorea.snapchat.InboxFragment.addFriend(InboxFragment.java:132)
at android.larrimorea.snapchat.InboxFragment$7.onClick(InboxFragment.java:248)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:153)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)

From this piece of code:
BackendlessUser curUser = Backendless.UserService.CurrentUser();

BackendlessUser[] friends = (BackendlessUser[])curUser.getProperty(“friends”);

Before you cast a relation property from BackendlessUser object, make sure to check the length of the array. If it is zero, cast will not work because internally that object was created as Object[ 0 ] and not a strongly-typed array. This is the only reason why cast will fail. However, if the collection is not empty, then a proper array will be created. We have never issued any patches for this problem, this is how it worked all along.

BackendlessUser user = Backendless.UserService.CurrentUser(); 
Object[] obj = (Object[]) backendlessUserItem.getProperty(BackendlessUserProperties.PENDINGS);
if (obj != null) { 
 BackendlessUser[] pendings = (BackendlessUser[]) obj; 
 ArrayList<BackendlessUser> pendingList = new ArrayList<BackendlessUser>(); 
 pendingList.addAll(Arrays.asList(pendings)); 
 pendingList.add(backendlessUserItem); 
 
 user.setProperty(BackendlessUserProperties.PENDINGS, pendingList); 
 Backendless.UserService.update(user, new DefaultCallback<BackendlessUser>(context)); 
} 
 
 



I tried this but there doesn’t seem to be any difference, I get the same error.

That’s not what I suggested to do. I wrote “make sure to check the length of the array”, but what you do is check if it is not null:

if (obj != null) {

It is not going to be null, it will either nave no elements a thus will NOT cast to BackendlessUser[] or will have more than 1 element and in that case you will be able to cast it.

Thanks, it works correctly now.
I will put the code here just for future reference.

BackendlessUser user = Backendless.UserService.CurrentUser();
Object[] obj = (Object[]) backendlessUserItem.getProperty(BackendlessUserProperties.PENDINGS);
if (obj.length > 0) {
    BackendlessUser[] pendings = (BackendlessUser[]) obj;
    ArrayList<BackendlessUser> pendingList = new ArrayList<BackendlessUser>();
    pendingList.addAll(Arrays.asList(pendings));
    pendingList.add(backendlessUserItem);

    user.setProperty(BackendlessUserProperties.PENDINGS, pendingList);
    Backendless.UserService.update(user, new DefaultCallback<BackendlessUser>(context));
} else {

    ArrayList<BackendlessUser> pendingList = new ArrayList<BackendlessUser>();
    pendingList.add(backendlessUserItem);

    user.setProperty(BackendlessUserProperties.PENDINGS, pendingList);
    Backendless.UserService.update(user, new DefaultCallback<BackendlessUser>(context));
}