Backendless Support
 
Solved

null response on saveAsync

Hello.

I am having a problem during save in android Backendless 4.

I have this.

  1. private void registrar() {

    mEndereco.setNomeDaRua(mEditTextEndereco.getText().toString());

    mEndereco.setBairro(mEditTextBairro.getText().toString());

    mEndereco.setCep(mEditTextCEP.getText().toString());

    mEndereco.setNumero(mEditTextNumero.getText().toString());

    mEndereco.setComplemento(mEditTextComplemento.getText().toString());

    mEndereco.setCidade(mEditTextCidade.getText().toString());

    mEndereco.setEstado("RJ");

    mEndereco.setReferencia(mEditTextReferencia.getText().toString());

    ArrayList<Endereco> enderecos = new ArrayList<>();

    enderecos.add(mEndereco);

    mPessoa.setEnderecoDoCliente(enderecos);

    Log.d(TAG, "Usuário final: " + mPessoa.toString());

    try

    {

    mEndereco.saveAsync(new AsyncCallback<Endereco>() {

    @Override

    public void handleResponse(Endereco response) {

    Log.d(TAG, response.getNomeDaRua());

    }

    @Override

    public void handleFault(BackendlessFault fault) {

    }

    });

    }

    catch ( Exception e )

    {

    Toast.makeText( this, e.getMessage(), Toast.LENGTH_SHORT ).show();

    return;

    }

    }

And my class is.

  1. public class Endereco implements Parcelable {

    @SuppressWarnings("unused")

    public static final Parcelable.Creator<Endereco> CREATOR = new Parcelable.Creator<Endereco>() {

    @Override

    public Endereco createFromParcel(Parcel in) {

    return new Endereco(in);

    }

    @Override

    public Endereco[] newArray(int size) {

    return new Endereco[size];

    }

    };

    protected Endereco(Parcel in) {

    numero = in.readString();

    ownerId = in.readString();

    referencia = in.readString();

    complemento = in.readString();

    nomeDaRua = in.readString();

    objectId = in.readString();

    estado = in.readString();

    cidade = in.readString();

    long tmpUpdated = in.readLong();

    updated = tmpUpdated != -1 ? new Date(tmpUpdated) : null;

    long tmpCreated = in.readLong();

    created = tmpCreated != -1 ? new Date(tmpCreated) : null;

    bairro = in.readString();

    cep = in.readString();

    }

    private String numero;

    private String ownerId;

    private String referencia;

    private String complemento;

    private String nomeDaRua;

    private String objectId;

    private String estado;

    private String cidade;

    private Date updated;

    private Date created;

    private String bairro;

    private String cep;

    public Endereco() {

    }

    public static Endereco findById(String id) {

    return Backendless.Data.of(Endereco.class).findById(id);

    }

    public static void findByIdAsync(String id, AsyncCallback<Endereco> callback) {

    Backendless.Data.of(Endereco.class).findById(id, callback);

    }

    public static Endereco findFirst() {

    return Backendless.Data.of(Endereco.class).findFirst();

    }

    public static void findFirstAsync(AsyncCallback<Endereco> callback) {

    Backendless.Data.of(Endereco.class).findFirst(callback);

    }

    public static Endereco findLast() {

    return Backendless.Data.of(Endereco.class).findLast();

    }

    public static void findLastAsync(AsyncCallback<Endereco> callback) {

    Backendless.Data.of(Endereco.class).findLast(callback);

    }

    public static List<Endereco> find(DataQueryBuilder queryBuilder) {

    return Backendless.Data.of(Endereco.class).find(queryBuilder);

    }

    public static void findAsync(DataQueryBuilder queryBuilder, AsyncCallback<List<Endereco>> callback) {

    Backendless.Data.of(Endereco.class).find(queryBuilder, callback);

    }

    public String getNumero() {

    return numero;

    }

    public void setNumero(String numero) {

    this.numero = numero;

    }

    public String getOwnerId() {

    return ownerId;

    }

    public String getReferencia() {

    return referencia;

    }

    public void setReferencia(String referencia) {

    this.referencia = referencia;

    }

    public String getComplemento() {

    return complemento;

    }

    public void setComplemento(String complemento) {

    this.complemento = complemento;

    }

    public String getNomeDaRua() {

    return nomeDaRua;

    }

    public void setNomeDaRua(String nomeDaRua) {

    this.nomeDaRua = nomeDaRua;

    }

    public String getObjectId() {

    return objectId;

    }

    public String getEstado() {

    return estado;

    }

    public void setEstado(String estado) {

    this.estado = estado;

    }

    public String getCidade() {

    return cidade;

    }

    public void setCidade(String cidade) {

    this.cidade = cidade;

    }

    public java.util.Date getUpdated() {

    return updated;

    }

    public java.util.Date getCreated() {

    return created;

    }

    public String getBairro() {

    return bairro;

    }

    public void setBairro(String bairro) {

    this.bairro = bairro;

    }

    public String getCep() {

    return cep;

    }

    public void setCep(String cep) {

    this.cep = cep;

    }

    public Endereco save() {

    return Backendless.Data.of(Endereco.class).save(this);

    }

    public void saveAsync(AsyncCallback<Endereco> callback) {

    Backendless.Data.of(Endereco.class).save(this, callback);

    }

    public Long remove() {

    return Backendless.Data.of(Endereco.class).remove(this);

    }

    public void removeAsync(AsyncCallback<Long> callback) {

    Backendless.Data.of(Endereco.class).remove(this, callback);

    }

    @Override

    public int describeContents() {

    return 0;

    }

    @Override

    public void writeToParcel(Parcel dest, int flags) {

    dest.writeString(numero);

    dest.writeString(ownerId);

    dest.writeString(referencia);

    dest.writeString(complemento);

    dest.writeString(nomeDaRua);

    dest.writeString(objectId);

    dest.writeString(estado);

    dest.writeString(cidade);

    dest.writeLong(updated != null ? updated.getTime() : -1L);

    dest.writeLong(created != null ? created.getTime() : -1L);

    dest.writeString(bairro);

    dest.writeString(cep);

    }

    }

When i debug, at

Log.d(TAG, "Usuário final: " + mPessoa.toString());

i have the Endereco class all filled right.

But, afther mEndereco.saveAsync(new AsyncCallback<Endereco>() {...

At: Log.d(TAG, response.getNomeDaRua()), i got a null at response

I am migrating to 3.x to 4.x, and done all change at init ands keys.

Obs.:

Pessoa mPessoa = new Pessoa();

Endereco mEndereco = new Endereco();

At global of the class.

Best Answer
photo

Your table name is "teste", while the class name is "Teste". They must be the same, OR, use the following code before you save the object:

Backendless.Data.mapTableToClass( "teste", Teste.class );

I just ran the following code and it saved the object without any problems:

  1. public class Main
  2. {
  3. public static void main( String[] args ) throws Throwable
  4. {
  5. Backendless.initApp( "0CB80A86-6D70-37D7-FFBA-E22B21BB4300", "YOUR-ANDROID-SECRET-KEY" );
  6. Backendless.Data.mapTableToClass( "teste", Teste.class );
  7. Teste test = new Teste();
  8. test.setNome( "Mark" );
  9. Backendless.Data.of( Teste.class ).save( test, new AsyncCallback<Teste>()
  10. {
  11. @Override
  12. public void handleResponse( Teste response )
  13. {
  14. System.out.println( "object saved. " + response.getNome() );
  15. }
  16. @Override
  17. public void handleFault( BackendlessFault fault )
  18. {
  19. System.out.println( fault.getDetail() );
  20. }
  21. } );
  22. }
  23. }

Leave a Comment

Comments (14)

photo
1

is there "nomeDaRua" value before you save the object? Have you checked that in debugger?

is the value for "nomeDaRua" saved in the database?

photo
1

nothing is saved and afther that .save nothing got save.

If you need any piece of code just ask.

But all values from Endereço is saved at the object, so mPessoa.getEndereco.getNomeDaRua have a valid String.

photo
1

If it is not saved, it means your app is not putting any value into that property. Please debug your code to make sure you're actually passing a value into the setNomeDaRua method

photo
1

Here i got the Log.d(TAG, "Usuário final: " + mPessoa.toString());

  1. Usuário final: Pessoa{created=null, updated=null, objectId='null', permissao='CLIENTE', email='a@a.com', ownerId='null', telefone='21987412868', password='1', nome='Rogério', enderecoDoCliente=[Endereco{numero='99', ownerId='null', referencia='brt', complemento='', nomeDaRua='Av Jaime Poggi', objectId='null', estado='RJ', cidade='Rio de Janeiro', updated=null, created=null, bairro='Barra', cep='22620311'}]}

As i can see it is all right, right?

  1. public String toString() {
  2. return "Pessoa{" +
  3. "created=" + created +
  4. ", updated=" + updated +
  5. ", objectId='" + objectId + '\'' +
  6. ", permissao='" + permissao + '\'' +
  7. ", email='" + email + '\'' +
  8. ", ownerId='" + ownerId + '\'' +
  9. ", telefone='" + telefone + '\'' +
  10. ", password='" + password + '\'' +
  11. ", nome='" + nome + '\'' +
  12. ", enderecoDoCliente=" + enderecoDoCliente +
  13. '}';
  14. }

photo
1

I also put a log at Endereco.java class

  1. public void saveAsync(AsyncCallback<Endereco> callback) {
  2. Log.d(TAG, this.toString());
  3. Backendless.Data.of(Endereco.class).save(this, callback);
  4. }

Got this:

  1. Endereco{numero='99', ownerId='null', referencia='brt', complemento='507', nomeDaRua='Av jaimr', objectId='null', estado='RJ', cidade='Rio de Janeiro', updated=null, created=null, bairro='bRra', cep='22620311'}

but nothing got saved. and still with null return at response;

photo
1

I see what's going on. Your "mPessoa" object references another object - "enderecoDoCliente". In Backendless 4 you cannot save both parent and child objects in the same "save" call. You should save "mPessoa" first, then save "enderecoDoCliente" and then create a relationship between them. Here's the documentation:

https://backendless.com/docs/android/doc.html#related_objects

https://backendless.com/docs/android/doc.html#data_relations_api_set_add_android

photo
1

I will try that, will take a time to do it, I will come back :P

photo
1

I made somethin simple and still getting erro.

  1. public class Teste {
  2. private String nome;
  3. public String getNome() {
  4. return nome;
  5. }
  6. public void setNome(String nome) {
  7. this.nome = nome;
  8. }
  9. public Teste() {
  10. }
  11. }

And put this at a button.

  1. Teste teste = new Teste();
  2. teste.setNome("Togerio");
  3. try
  4. {
  5. Backendless.Data.of(Teste.class).save(teste, new AsyncCallback<Teste>() {
  6. @Override
  7. public void handleResponse(Teste response) {
  8. Log.d(TAG, response.getNome());
  9. }
  10. @Override
  11. public void handleFault(BackendlessFault fault) {
  12. }
  13. });
  14. }
  15. catch ( Exception e )
  16. {
  17. Toast.makeText( this, e.getMessage(), Toast.LENGTH_SHORT ).show();
  18. return;
  19. }

Still getting null obj as response.

  1. java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String br.com.programadorjava.servicoscorporativos.entidades.Teste.getNome()' on a null object reference

photo
1

What does class Teste look like?

photo
1

it is there at the comment.

  1. public class Teste {
  2. private String nome;
  3. public String getNome() {
  4. return nome;
  5. }
  6. public void setNome(String nome) {
  7. this.nome = nome;
  8. }
  9. public Teste() {
  10. }
  11. }

photo
1

What is your app id?

photo
1

0CB80A86-6D70-37D7-FFBA-E22B21BB4300

photo
2

Your table name is "teste", while the class name is "Teste". They must be the same, OR, use the following code before you save the object:

Backendless.Data.mapTableToClass( "teste", Teste.class );

I just ran the following code and it saved the object without any problems:

  1. public class Main
  2. {
  3. public static void main( String[] args ) throws Throwable
  4. {
  5. Backendless.initApp( "0CB80A86-6D70-37D7-FFBA-E22B21BB4300", "YOUR-ANDROID-SECRET-KEY" );
  6. Backendless.Data.mapTableToClass( "teste", Teste.class );
  7. Teste test = new Teste();
  8. test.setNome( "Mark" );
  9. Backendless.Data.of( Teste.class ).save( test, new AsyncCallback<Teste>()
  10. {
  11. @Override
  12. public void handleResponse( Teste response )
  13. {
  14. System.out.println( "object saved. " + response.getNome() );
  15. }
  16. @Override
  17. public void handleFault( BackendlessFault fault )
  18. {
  19. System.out.println( fault.getDetail() );
  20. }
  21. } );
  22. }
  23. }

photo
1

Just copy yout code and execute fine. wear... but thx for explain more about hoe save relation, it work perfect.