Cannot Remove/Update Person Data relationship

I am currently trying to implement a add contact and do a remove from their respective requesting contact list. I cannot seem to add another person to a persons contact list and and remove the same person from the requesting list (this is being done on two people who have their own requesting and contacts lists). What I am doing is I load the relationship data into each person and then do a remove and an update/save, then I add to their contacts list and another save/update, but its throwing this error: Caused by: java.lang.NullPointerException: Attempt to invoke interface method ‘java.util.Iterator java.util.Collection.iterator()’ on a null object reference My problem is a simple case of removing a person from one list and into another.
Heres is my Code:

Person tempOtherPerson = personsRequestsList.get(position); // Get the person I want to add as contact

ArrayList<String> relationProps = new ArrayList<String>();
relationProps.add(“contacts”);
relationProps.add(“personsImRequesting”);
Backendless.Persistence.of(Person.class).loadRelations(tempOtherPerson, relationProps);

ArrayList<String> relationProps1 = new ArrayList<String>();
relationProps1.add(“contacts”);
relationProps1.add(“personsRequestingMe”);
Backendless.Persistence.of(Person.class).loadRelations(personLoggedIn, relationProps1);

personLoggedIn.addContact(tempOtherPerson); // Add as a contact
tempOtherPerson.addContact(personLoggedIn); // Add as a contact

Backendless.Data.save(personLoggedIn); // Save logged in persons with added contact back to backendless

personLoggedIn.removePersonRequestingMe(tempOtherPerson); // Now remove person from the requesting list
personLoggedIn = Backendless.Persistence.save(personLoggedIn); // Save back again with person removed from list

tempOtherPerson = Backendless.Persistence.save(tempOtherPerson); // Save otherperson with new contact also
tempOtherPerson.removePersonImRequesting(personLoggedIn); // …now remove add contact from requesting list
tempOtherPerson = Backendless.Persistence.save(tempOtherPerson); // Save again

And here is the class methods for the Person class doing the adding and removing:

/**

  • Method called on non-logged in user to remove person wanting to be contacts with
  • @param person
  • @return boolean whether user has been removed
    */public boolean removePersonImRequesting(Person person) {
    Iterator i = personsImRequesting.iterator();
    int j = 0;
    while (i.hasNext()) {
    Person element = (Person) i.next();
    if (element.getObjectId().equals(person.getObjectId())) {
    // personsImRequesting.remove(addingContact);
    personsImRequesting.remove(j);
    return true;
    }
    j++;
    }
    return false;
    }

//Adds some one to my contacts listpublic void addContact(Person contact) {
if (contacts == null) {
contacts = new ArrayList<Person>();
}
contacts.add(contact);
}

/**

  • Method called on logged in user to remove person who is requesting me as contact from personRequestingMe List
  • @param person
  • @return boolean whether user has been removed
    */
    public boolean removePersonRequestingMe(Person person) {
    Iterator i = personsRequestingMe.iterator();
    int j = 0;
    while (i.hasNext()) {
    Person element = (Person) i.next();
    if (element.getObjectId().equals(person.getObjectId())) {
    // personsImRequesting.remove(addingContact);
    personsRequestingMe.remove(j);
    return true;
    }
    j++;
    }
    return false;
    }

Its kinda partially working, it will add as a contact for one but the not the other and both requesting lists remain unchanged.

Would it be worth creating an API that deals with these kind of situations, where a request is sent that is either rejected or approved by the recipient?

Hi Leonard,

I looked through the code and do not understand the following two methods from class Person:

iterator() and remove().

Can you show their implementation please?

Regards,
Mark

I dont have my own implementation of these two methods, the two methods are predefined java methods. The iterator method will will iterator through the list of elements returning the next element, Iterators allow the caller to remove elements from the underlying collection during the iteration. And the java.util.ArrayList.remove(int index) method removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). When I debug the app it goes through the list then removes the appropriate item (person), but its just not saving it properly at this stage.
Thanks.

I misread the name of the variable. You have “personsRequestingMe” and I read “personRequestingMe”. Sorry, my bad.

If you’re getting an NPE when you call the iterator() method, clearly the collection is null. Have you tried setting a breakpoint to see why the collection is not initialized or perhaps where it is reset?

Mark

I see. I have just run the app now and its not throwing any error (it must have been from when I didn’t manually set up the tables again with entries I think, so there is no exceptions being thrown and runs fine), however only the contact lists of both persons are adding each other and updating. The requesting lists of the two persons still show the other person they are requesting. Im not sure if its in the order I am saving the objects back to backendless? Would this make a difference?

I do not think the order should make any difference. Try setting the breakpoint right after you save a person with the updated “personsRequestingMe” collection and check in console what the updated object looks like.

Checked with the point break, the personsRequestiongMe list has a size = 0 from the returned object from the save method on the personLoggedIn and contact has added for the contact (so the two methods worked), but on the console only the contact is added but the person in the requesting list still remains.

Here’s what I think is going on. See my comments above each line:

// at this point "tempOtherPerson" has the relations. 
// Which means when you save it, any changes to the relations will be saved
tempOtherPerson = Backendless.Persistence.save(tempOtherPerson); 


// at this point you are using "tempOtherPerson" which you got 
// from the result of the "save" operation. ALL the relations which you loaded
// earlier will be gone. Hence removing "personLoggedIn" from the
// requesting list does nothing
tempOtherPerson.removePersonImRequesting(personLoggedIn); 


// since the previous operation didn't do anything, this one will not change
// the related collection.
tempOtherPerson = Backendless.Persistence.save(tempOtherPerson); 

I slightly suspected that could the case, I have tried isolating the issue by simply trying to just do a removal for a list and then do an update. I am trying to remove a person from a requesting persons list and then update on the backend. It seem to work fine on the app when I debug it with breakpoints but not on the console on the backend. It is not throwing any exceptions. Heres my code for this:

// Get the person I want to add as contact and remove from my requesting list and his
Person tempOtherPerson = personsRequestsList.get(position);

// Setup Relations
ArrayList<String> relationProps = new ArrayList<String>();
relationProps.add(“personsImRequesting”);
Backendless.Data.of(Person.class).loadRelations(tempOtherPerson, relationProps);

// Im am just removing myself (personloggedin) from the other persons list since
//tempOtherPeron is requesting me
tempOtherPerson.removePersonImRequesting(personLoggedIn);

// save with update
Backendless.Data.save(tempOtherPerson);

Could you please clarify what you mean by “but not on the console on the backend”?

Console on the backend, I meant to say on the backend data browser.

You mean Backendless Console? The Data screen? Can you be more specific when you say it is not working? For instance, if you could provide steps to reproduce (as a number list), it would be very helpful.

Regards,
Mark

I have attached some screenshots of the problem with comments. The issue is its not updating on the backend when I try to update things (more specifically removing items) on the backend with the save method. Thanks.
Leonard.

Please see attached screenshot. There are two things I am requesting in there (you will see them in the image).

Attached are the screen shots. Thanks Mark.

Could you please attach the source code for your Person class?

public class Person {
    public String fname;
    public String lname;
    public String fullname;
    public Person mecontact;
    public List&lt;Person&gt; contacts;
    public String objectId;
    public String ownerId;
    public String phone;
    public String email;
    public String locationstring;
    public List&lt;Person&gt; personsImRequesting;
    public List&lt;Person&gt; personsRequestingMe;

    public List&lt;Slot&gt; pendingResponseSlot;
    public List&lt;Slot&gt; myCreatedSlot;
    public List&lt;Slot&gt; goingtoslots;

    public List&lt;Person&gt; getPersonsImRequesting() {
        return personsImRequesting;
    }

    public boolean removeContactForBoth(Person personToRemove) {

        List&lt;Person&gt; otherList = personToRemove.contacts;
        int otherListSize = otherList.size() - 1;
        int myListSize = contacts.size() - 1;

        boolean imRemovedFromHisList = false; // Found, not removed
        boolean hesRemovedFromMyList = false;

        int indexInMyList = 0;
        int indexInHisList = 0;

        int j = 0;

        //Check we dont search index higher then either list size and that contacts have been removed from each list
        while (((!imRemovedFromHisList) || (!hesRemovedFromMyList)) && ((j <= otherListSize) || (j <= myListSize))) {


            if (!imRemovedFromHisList) {
                if (objectId.equals(otherList.get(j).objectId)) {

                    //otherList.remove(j);
                    indexInHisList = j;
                    imRemovedFromHisList = true;
                }
            }

            if (!hesRemovedFromMyList) {
                if (personToRemove.getObjectId().equals(contacts.get(j).objectId)) {

                    //contacts.remove(j);
                    indexInMyList = j;
                    hesRemovedFromMyList = true;
                }
            }
            j++;
        }

        if (imRemovedFromHisList && hesRemovedFromMyList) {

            contacts.remove(indexInMyList);
            otherList.remove(indexInHisList);
            return true;

        } else {
            return false;
        }
    }


    public void addPersonsRequestingMe(Person contact) {
        if (personsRequestingMe == null)
            personsRequestingMe = new ArrayList&lt;Person&gt;();
        personsRequestingMe.add(contact);
    }


    /***
     * Method called on non-logged in user to remove person being requested as contact from personImRequesting List
     *
     * @param person
     * @return
     */
    public boolean removePersonImRequesting(Person person) {

        Iterator i = personsImRequesting.iterator();

        int j = 0;
        while (i.hasNext()) {
            Person element = (Person) i.next();

            if (element.getObjectId().equals(person.getObjectId())) {

                //  personsImRequesting.remove(addingContact);

                personsImRequesting.remove(j);
                return true;
            }
            j++;
        }
        return false;
    }

    public boolean removeContact(Person person) {

        Iterator i = contacts.iterator();

        int j = 0;
        while (i.hasNext()) {
            Person element = (Person) i.next();

            if (element.getObjectId().equals(person.getObjectId())) {

                //  personsImRequesting.remove(addingContact);

                contacts.remove(j);
                return true;
            }
            j++;
        }
        return false;
    }


    /**
     * Method called on logged in user to remove person who is requesting me as contact from personRequestingMe List
     *
     * @param person
     * @return boolean whether user has been removed
     */
    public boolean removePersonRequestingMe(Person person) {

        Iterator i = personsRequestingMe.iterator();

        int j = 0;
        while (i.hasNext()) {
            Person element = (Person) i.next();

            if (element.getObjectId().equals(person.getObjectId())) {

                //  personsImRequesting.remove(addingContact);

                personsRequestingMe.remove(j);
                return true;
            }
            j++;
        }
        return false;
    }


    public void addToImRequesting(Person otherRequested) {
        personsImRequesting.add(otherRequested);
    }

    public void addToRequestingMe(Person otherRequested) {

    }

    /**
     * @param addingContact
     * @return 0 Added to my requesting list. 1 if contact removed from requesting. 2 if contact is already a contact.
     */
    public Integer addPersonsImRequesting(Person addingContact) {
        if (!contacts.contains(addingContact)) {

            //  personsImRequesting.iterator()

            boolean removed = false;
            Iterator i = personsImRequesting.iterator();
            int j = 0;
            while (i.hasNext()) {
                Person element = (Person) i.next();

                if (element.getObjectId().equals(addingContact.getObjectId())) {

                    //  personsImRequesting.remove(addingContact);

                    personsImRequesting.remove(j);
                    removed = true;
                    return 1;
                }
                j++;
            }
            if (!removed) {
                personsImRequesting.add(addingContact);
                return 0;
            }
        }
        return 2;
    }

    public void setPersonsRequestingMe(List&lt;Person&gt; personsrequestingme) {
        this.personsRequestingMe = personsrequestingme;
    }

    public void setPersonsImRequesting(List&lt;Person&gt; personsimrequesting) {
        this.personsImRequesting = personsimrequesting;
    }

    public List&lt;Person&gt; getPersonsRequestingMe() {
        return personsRequestingMe;
    }

    public void addSlotToPendingResponseSlot(Slot slotItem) {
        if (pendingResponseSlot == null)
            pendingResponseSlot = new ArrayList&lt;Slot&gt;();
        pendingResponseSlot.add(slotItem);
    }

    public void addContact(Person contact) {
        if (contacts == null) {
            contacts = new ArrayList&lt;Person&gt;();
        }
        contacts.add(contact);
    }

    public void addSlotToMyCreatedSlot(Slot slotItem) {
        if (myCreatedSlot == null)
            myCreatedSlot = new ArrayList&lt;Slot&gt;();
        myCreatedSlot.add(slotItem);
    }

    public void addSlotGoingToSlot(Slot slotItem) {
        if (goingtoslots == null)
            goingtoslots = new ArrayList&lt;Slot&gt;();
        goingtoslots.add(slotItem);
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getFullname() {
        return fullname;
    }

    public List&lt;Slot&gt; getMyCreatedSlot() {
        return myCreatedSlot;
    }

    public void setMyCreatedSlot(List&lt;Slot&gt; mycreatedslot) {
        this.myCreatedSlot = mycreatedslot;
    }

    public List&lt;Slot&gt; getGoingToSlot() {
        return goingtoslots;
    }

    public List&lt;Slot&gt; getPendingResponseSlot() {
        return pendingResponseSlot;
    }

    public void setGoingToSlot(List&lt;Slot&gt; goingtoslot) {
        this.goingtoslots = goingtoslot;
    }

    public void setPendingResponseSlot(List&lt;Slot&gt; pendingresponseslot) {
        this.pendingResponseSlot = pendingresponseslot;
    }


    //TODO Document Code methods

    //TODO Missing some columns not used... just a note.


    public void setLocationstring(String locationstring) {
        this.locationstring = locationstring;
    }

    public String getLocationstring() {
        return locationstring;
    }


    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmail() {
        return email;
    }

    public List&lt;Person&gt; getContacts() {
        return contacts;
    }

    public void setContacts(List&lt;Person&gt; contacts) {
        this.contacts = contacts;
    }

    public Person getMecontact() {
        return mecontact;
    }

    public void setMeAsContact(Person mecontact) {
        this.mecontact = mecontact;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(String ownerId) {
        this.ownerId = ownerId;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public String getObjectId() {
        return objectId;
    }
}


Hi mark, just wondering if you were able to look into/solve the issue I was having with removing data entries with updates? Cheers Leonard

Ive managed to solve the issue. Cheers