Error while updating a DataRelation (1:N)

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

Hi Gabriele,

The following block of code does not make sense:

for (int i = 0; i< currentFriends.length - 1; i++){
newFriends = currentFriends;
}

Please correct that problem first (you would be better of using the System.arrayCopy instead) and then see if you still have a problem.

Regards,
Mark

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.

Could you please attach a screenshot of the code so we can see what it looks like as-is?

It continue to cut the brackets:

newFriends [ i ] = currentFriends [ i ];

http://support.backendless.com/public/attachments/6992858d2225b07144754d8aad05097a.PNG&lt;/img&gt;

6992858d2225b07144754d8aad05097a.PNG

Thanks. Could you please show the entire stack trace for the error you’re getting?

Yes, sure. Is it in fault.getCode() ?

See what you get with failt.getDetail()

http://support.backendless.com/public/attachments/a6a0b91a950a9d1f33cc36ba2f6620db.PNG&lt;/img&gt;

Using this code, I get that message is empty:

03-17 20:18:42.384 6979-6979/… E/Error: Error 3 on testuserB
03-17 20:18:42.384 6979-6979/… E/Error: null

a6a0b91a950a9d1f33cc36ba2f6620db.PNG

Could you try the following please?:

user.setProperty(“friends”, Arrays.asList( newFriends ) );

Please let me know if that works.

It still gives the same error

Just to confirm - the error occurs when the user1 object is retrieved from the server. Is that correct?

In that case, how is the “friends” collection loaded? Do you have auto-load enabled for that property?

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.

http://support.backendless.com/public/attachments/f3a42f603686c9d424b8f4c48d900a60.PNG&lt;/img&gt;

I checked the option autoload

Thanks, this helps. And one more question: what is the version of the Backendless SDK for Android you’re using?

To be fair I don’t know, I started with the ResturantToGo tutorial, and then I developed my app from that. Where can I see the version?

That would be a very old library. Could you update to the latest 3.x library? You can get it from maven:
https://search.maven.org/#artifactdetails|com.backendless|backendless|3.0.25|jar

Ok, thanks I’ll try the to update the library and then I’ll comment again if the error persists