How to save null value in data API from my android app

I have this simple code

if (phoneList.size()>=1){

        if (contact.getTelephone1()!=null){

            contact.setTelephone1((contact.getTelephone1().equals(phoneList.get(0)))? 
             contact.getTelephone1():phoneList.get(0));
        }else {
            contact.setTelephone1(phoneList.get(0));
        }

        if (phoneList.size()==1 && contact.getTelephone2()!=null){
            contact.setTelephone2(null);
            contact.setTelephone3(null);
        }
    }

if my phoneList size equals 0 the object fields telephone2 and telephone3 is set to null but when i save contact object telephone2 and telephone3 is not set to null in the database.

Backendless database does not use null values?

Hi Rholde,

What does the Contact class look like? What are the data types for telephone2 and telephone3 columns in the database?

Regards,
Mark

public class Contacts implements Parcelable ,Comparable
{
private String objectId;

private String nom;
private String poste;
private String password;
private String email1 = null;
private String email2 = null;
private String email3 = null;
private String telephone1 = null;
private String telephone2 = null;
private String telephone3 = null;
private String direction;
private String service;
private java.util.Date updated;
private Double serialVersionUID;
private java.util.Date created;
private String ownerId;

public Contacts(){}

protected Contacts(Parcel in) {
objectId = in.readString();
nom = in.readString();
poste = in.readString();
password = in.readString();
email1 = in.readString();
email2 = in.readString();
email3 = in.readString();
telephone1 = in.readString();
telephone2 = in.readString();
telephone3 = in.readString();
direction = in.readString();
service = in.readString();
if (in.readByte() == 0) {
serialVersionUID = null;
} else {
serialVersionUID = in.readDouble();
}
ownerId = in.readString();
}

public static final Creator CREATOR = new Creator() {
@Override
public Contacts createFromParcel(Parcel in) {
return new Contacts(in);
}

@Override
public Contacts[] newArray(int size) {
  return new Contacts[size];
}

};

public String getObjectId()
{
return objectId;
}

public String getTelephone2()
{
return telephone2;
}

public void setTelephone2( String telephone2 )
{
this.telephone2 = telephone2;
}

public String getNom()
{
return nom;
}

public void setNom( String nom )
{
this.nom = nom;
}

public String getPoste()
{
return poste;
}

public void setPoste( String poste )
{
this.poste = poste;
}

public String getPassword()
{
return password;
}

public void setPassword( String password )
{
this.password = password;
}

public String getEmail1()
{
return email1;
}

public void setEmail1( String email1 )
{
this.email1 = email1;
}

public String getTelephone1()
{
return telephone1;
}

public void setTelephone1( String telephone1 )
{
this.telephone1 = telephone1;
}

public String getEmail2()
{
return email2;
}

public void setEmail2( String email2 )
{
this.email2 = email2;
}

public String getEmail3()
{
return email3;
}

public void setEmail3( String email3 )
{
this.email3 = email3;
}

public String getDirection()
{
return direction;
}

public void setDirection( String direction )
{
this.direction = direction;
}

public String getService()
{
return service;
}

public void setService( String service )
{
this.service = service;
}

public java.util.Date getUpdated()
{
return updated;
}

public Double getSerialVersionUID()
{
return serialVersionUID;
}

public void setSerialVersionUID( Double serialVersionUID )
{
this.serialVersionUID = serialVersionUID;
}

public String getTelephone3()
{
return telephone3;
}

public void setTelephone3( String telephone3 )
{
this.telephone3 = telephone3;
}

public java.util.Date getCreated()
{
return created;
}

public String getOwnerId()
{
return ownerId;
}

public Contacts save()
{
return Backendless.Data.of( Contacts.class ).save( this );
}

public void saveAsync( AsyncCallback callback )
{
Backendless.Data.of( Contacts.class ).save( this, callback );
}

public Long remove()
{
return Backendless.Data.of( Contacts.class ).remove( this );
}

public void removeAsync( AsyncCallback callback )
{
Backendless.Data.of( Contacts.class ).remove( this, callback );
}

public static Contacts findById( String id )
{
return Backendless.Data.of( Contacts.class ).findById( id );
}

public static void findByIdAsync( String id, AsyncCallback callback )
{
Backendless.Data.of( Contacts.class ).findById( id, callback );
}

public static Contacts findFirst()
{
return Backendless.Data.of( Contacts.class ).findFirst();
}

public static void findFirstAsync( AsyncCallback callback )
{
Backendless.Data.of( Contacts.class ).findFirst( callback );
}

public static Contacts findLast()
{
return Backendless.Data.of( Contacts.class ).findLast();
}

public static void findLastAsync( AsyncCallback callback )
{
Backendless.Data.of( Contacts.class ).findLast( callback );
}

public static List find( DataQueryBuilder queryBuilder )
{
return Backendless.Data.of( Contacts.class ).find( queryBuilder );
}

public static void findAsync( DataQueryBuilder queryBuilder, AsyncCallback<List> callback )
{
Backendless.Data.of( Contacts.class ).find( queryBuilder, callback );
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(objectId);
dest.writeString(nom);
dest.writeString(poste);
dest.writeString(password);
dest.writeString(email1);
dest.writeString(email2);
dest.writeString(email3);
dest.writeString(telephone1);
dest.writeString(telephone2);
dest.writeString(telephone3);
dest.writeString(direction);
dest.writeString(service);
if (serialVersionUID == null) {
dest.writeByte((byte) 0);
} else {
dest.writeByte((byte) 1);
dest.writeDouble(serialVersionUID);
}
dest.writeString(ownerId);
}

@Override
public String toString() {
return “Contacts{” +
“objectId=’” + objectId + ‘’’ +
“, nom=’” + nom + ‘’’ +
“, poste=’” + poste + ‘’’ +
“, password=’” + password + ‘’’ +
“, email1=’” + email1 + ‘’’ +
“, email2=’” + email2 + ‘’’ +
“, email3=’” + email3 + ‘’’ +
“, telephone1=’” + telephone1 + ‘’’ +
“, telephone2=’” + telephone2 + ‘’’ +
“, telephone3=’” + telephone3 + ‘’’ +
“, direction=’” + direction + ‘’’ +
“, service=’” + service + ‘’’ +
“, updated=” + updated +
“, serialVersionUID=” + serialVersionUID +
“, created=” + created +
“, ownerId=’” + ownerId + ‘’’ +
‘}’;
}

public int compareTo(Contacts autre) {
return this.getNom().compareTo(autre.getNom());
}
}

The datatype is String

Thanks, have you tried configuring the default value for the telephone2/3 columns in the database as NULL ?

I don’t know if it is possible to set default values to null

It only accetps

contact.setTelephone2("");

but does not recognize

contact.setTelephone2(null);

thanks

You’re right, you cannot set NULL as the default since it is the default value by default (sorry for triple “default” reference in the same sentence) :slight_smile:

What is the value saved as in the database when you set it as null on the client side?

it keeps the old value; does not set to null ;

unless i use contact.setTelephone2(“”); whichi don’t want

I know what’s going on. The client side ignores the properties which are null.

As a workaround, can you adopt another value (such as an empty string) as an indication that there is no value?