Support Topics Documentation Slack YouTube Blog

Using setRelation to add composite field value to parent


(Jason Brown) #1

Hey Guys, I’ve been trying to use composition in 2 object classes.

I’m trying to use the setRelation() method but It goes to the handle fault method and says I have a null pointer exception.

My 2 classes are Player(composite field in Match) and Match(parent)

Player class:

package com.brown.jason.testcomposition;

/**

  • Created by Obie on 2018/10/13.
    */

public class Player {
private String name;
private String surname;
private String objectId;

public String getObjectId() {
    return objectId;
}

public void setObjectId(String objectId) {
    this.objectId = objectId;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

public Player(String name, String surname){

    this.name = name;
    this.surname = surname;
}
public Player(){

}

}


Matches class:

package com.brown.jason.testcomposition;

/**

  • Created by Obie on 2018/10/13.
    */

public class Match {

private String match;
private Player player;
private String objectId;

public String getObjectId() {
    return objectId;
}

public void setObjectId(String objectId) {
    this.objectId = objectId;
}

public String getMatch() {
    return match;
}

public void setMatch(String match) {
    this.match = match;
}

public Player getPlayer() {
    return player;
}

public void setPlayer(Player player) {
    this.player = player;
}



public Match(String match, String name, String surname){



    this.match = match;
    this.player = new Player(name, surname);


}
public Match(){

    this.player = new Player();
}//default constructor

}

and this is what I’m trying to use as sample data inside my main activity:

package com.brown.jason.testcomposition;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.backendless.Backendless;
import com.backendless.BackendlessUser;
import com.backendless.async.callback.AsyncCallback;
import com.backendless.exceptions.BackendlessFault;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private Match match2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Match match = new Match();
    match.setMatch("BIG FAT MATCH");
    final TextView textView = (TextView)findViewById(R.id.textView);



    Player player = new Player("Jason", "Brown");
    ArrayList<Player> players = new ArrayList<>();
    players.add(player);

    Backendless.Data.of(Match.class).setRelation(match, "player", players, new AsyncCallback<Integer>() {
        @Override
        public void handleResponse(Integer response) {
            Toast.makeText(MainActivity.this, "it WOrked, I hope", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Toast.makeText(MainActivity.this, fault.getMessage(), Toast.LENGTH_SHORT).show();
            textView.setText(fault.getMessage());
        }
    });



}//end onCreate

}

I manually added the relation of Player in Matches with a 1:1 relation

Any Help would be appreciated!


(Jason Brown) #2

Follow up question, I might have figured this out. I’m not sure why though. I’m still unsure on how to use the data after the relation is made


(Jason Brown) #3

I checked the documentation, but queryBuilder.addRelation is not an option I get


(Sergey Kukurudzyak) #4

Hello,

You have to complete following steps yo set relation:

  1. Create Match object and save it to the backendless server
  2. Create Player object and save it to the backendless server
  3. For saved Match and Player objects call:
Backendless.Data.of(Match.class).setRelation(match, "player", players