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?