Backendless Support
 
Answered

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:

  1. updateFriendsList(user1, user2);

    Backendless.UserService.update(user1, new AsyncCallback<BackendlessUser>() {...}

where the function updateFriendsList has the following code:

  1. 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:

  1. 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

Leave a Comment

Comments (24)

photo
1

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

photo
1

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.

photo
1

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

photo
1

It continue to cut the brackets:

newFriends [ i ] = currentFriends [ i ];

photo
1

6992858d2225b07144754d8aad05097a

photo
1

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

photo
1

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

photo
1

See what you get with failt.getDetail()

photo
1

a6a0b91a950a9d1f33cc36ba2f6620db

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

photo
1

Could you try the following please?:

user.setProperty("friends", Arrays.asList( newFriends ) );

Please let me know if that works.

photo
1

It still gives the same error

photo
1

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?

photo
1

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

photo
1

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.

photo
1

f3a42f603686c9d424b8f4c48d900a60

I checked the option autoload

photo
1

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

photo
1

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?

photo
1

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%7Ccom.backendless%7Cbackendless%7C3.0.25%7Cjar

photo
1

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

photo
1

I updated the library to Backendless 3.0.25, but the error still persists

photo
1

Ok, this is good news, which means we should be able to reproduce it. What is your app id? We will try reproducing with your account.

photo
1

App id: FDAF16E8-1FC1-0CF5-FF00-C353D9172500

Thank you very much

photo
photo
1

I can not reproduce your issue here it is a code:

  1. Backendless.Data.of( BackendlessUser.class )

    .findById( "81DBC0DF-3B37-1EC4-FFC4-41E5AC9E0300", new AsyncCallback<BackendlessUser>()

    {

    public void handleResponse( final BackendlessUser foundUser )

    {

    System.out.println("User found");

    Backendless.Data.of( BackendlessUser.class )

    .findById( "836C6185-B26C-F61F-FFBA-C85C75635100", new AsyncCallback<BackendlessUser>()

    {

    public void handleResponse( BackendlessUser newFriend )

    {

    System.out.println("New friend found");

    BackendlessUser[] friends = (BackendlessUser[]) foundUser.getProperty( "friends" );

    if(friends == null)

    return;

    BackendlessUser[] newFriends = new BackendlessUser[friends.length + 1];

    int i;

    for( i = 0; i < newFriends.length-1; i++ )

    {

    newFriends = friends[ i ];

    }

    newFriends = newFriend;

    foundUser.setProperty( "friends", newFriends );

    Backendless.Data.of( BackendlessUser.class ).save( foundUser, new AsyncCallback<BackendlessUser>()

    {

    public void handleResponse( BackendlessUser backendlessUser )

    {

    System.out.println("Relation added");

    }

    public void handleFault( BackendlessFault backendlessFault )

    {

    }

    } );

    }

    public void handleFault( BackendlessFault backendlessFault )

    {

    }

    } );

    }

    public void handleFault( BackendlessFault backendlessFault )

    {

    }

    } );

and here it is screen shot with out http://take.ms/QamB3 also here its screen shot from Backendless console http://take.ms/ArIsJ

photo
1

Thank you. Substituting my code with yours it works, although I'm still not sure where I got wrong.

photo