Hi, in my android app I give the possibility to people to be friends with each other. I save the friend relationships with a Data Relation (1:N): Users. I change the user properties with a service in my app that run the following code:
updateFriendsList(user1, user2);
Backendless.UserService.update(user1, new AsyncCallback<BackendlessUser>() {...}
where the function updateFriendsList has the following code:
private void updateFriendsList (BackendlessUser user, BackendlessUser friend){
BackendlessUser[] newFriends;
Object[] currentFriendObject = (Object[]) user.getProperty("friends");
if (currentFriendObject.length > 0){
BackendlessUser[] currentFriends = (BackendlessUser[]) currentFriendObject;
newFriends = new BackendlessUser[currentFriends.length + 1];
for (int i = 0; i< currentFriends.length - 1; i++){
newFriends [ i ] = currentFriends [ i ];
}
newFriends[newFriends.length - 1] = friend;
} else {
newFriends = new BackendlessUser[]{ friend };
}
user.setProperty("friends", newFriends);
}
The program works only when it is setting the first relationship (therefore it goes in the else statement) while it is not working if it has to add the relationship to others. It produce the following error:
BackendlessFault{ code: 'IllegalArgumentException', message: 'Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference' }
Can anyone help me to solve this problem?
Thanks in advance,
Gabriele
Sorry, for some reason the copy and paste has cut out the brackets, my current code is:
newFriends [ i ]= currentFriends [ i ];
basically I’m creating a new array with the length increased by one, then coping all the elements in the original array in the one created to which, at the end, I add the user passed as friend.
I retrieve them from the server with a query update them and then trying to update the version on the server, in the screenshot the code before it gets the error
My question is how is the “friends” collection end up in the user object. The relation must be loaded there somehow - so I am asking how you’re loading the related objects.