Hello, I am trying to create a friends system yet I keep recieving this error from my Async callback fault message. java.lang.Object[] cannot be cast to java.util.Map. I narrowed down the problem to my updateFriendsList method which is shown below.
BTW i followed this tutorial and it seemed to work fine for the person who made the tutorial.
protected void updateFriendsList(BackendlessUser user, BackendlessUser friend){
//update users friend list to include friend
BackendlessUser[] newFriends;
Object[] currentFriendObjects = (Object[]) user.getProperty(“friends”);
if(currentFriendObjects.length>0){
BackendlessUser[] currentFriends = (BackendlessUser[]) currentFriendObjects;
newFriends = new BackendlessUser[currentFriends.length+1];
for (int i =0; i<currentFriends.length; i++){
newFriends = currentFriends;
}
newFriends[newFriends.length-1] = friend;
}else{
newFriends = new BackendlessUser[] {friend};
}
user.setProperty(“friends”,newFriends);
}
for (int i =0; i<currentFriends.length; i++){
newFriends = currentFriends;
}
You’re assigning the entire array to the “newFriends” variable as many times as many objects are in the currentFriends array.
Use either System.arrayCopy or indexes for the arrays.
Also, the code you shown does not save the user object, you simply assign a custom property.
hmm for some reason the index of the arrays are not showing up here… it’s actaully
newFriends(with index set at i) = currentFriends(with index set at i);
Hey Mark, i fixed the problem by changing the cast from Object to BackendlessUser… Everything seems to be working flawlessly but i see that your telling me to save the user object but it seems that without saving the object that my backendless database is being updated? so is their even a need to save?
Actually, now i have realized that when trying to add friends i am starting to get java.lang.Object[] cannot be cast to com.backendless.BackendlessUser[] for this line of code
BackendlessUser[] currentFriends = (BackendlessUser[]) user.getProperty(“friends”);
yet when i try to cast Object[] i get java.lang.Object[] cannot be cast to java.util.Map