Use seters methods within a void function

Asked

Viewed 67 times

0

I’m kind of new to java and I’m trying to create an application where I call server data and fill an object, but the object is not filled, whenever I call the object all attributes are null.

The server data call function is this:

public void preencherObj(Context context, final Aplicativo aplicativo){
    try{
        Ion.with(context)
            .load(Constantes.url)
            .setBodyParameter("id_empresa", String.valueOf(Constantes.id_empresa))
            .setBodyParameter("acao", "B")
            .asJsonObject()
            .setCallback(new FutureCallback<JsonObject>() {
                @Override
                public void onCompleted(Exception e, JsonObject result) {
                    aplicativo.setId_aplicativo(result.get("id_aplicativo").getAsInt());
                    aplicativo.setNm_cor(result.get("nm_cor").getAsString());
                    aplicativo.setNr_telefone(result.get("nr_telefone").getAsString());
                    aplicativo.setNr_telefone2(result.get("nr_telefone2").getAsString());
                    aplicativo.setUrl_logo(result.get("url_logo").getAsString());
                    aplicativo.setUrl_whats(result.get("url_whats").getAsString());
                    aplicativo.setUrl_local(result.get("url_local").getAsString());
                    aplicativo.setUrl_site(result.get("url_site").getAsString());
                    aplicativo.setNm_endereco(result.get("nm_endereco").getAsString());
                    aplicativo.setNm_cidade(result.get("nm_cidade").getAsString());
                    aplicativo.setNm_estado(result.get("nm_estado").getAsString());
                    aplicativo.setCd_cep(result.get("cd_cep").getAsString());
                    aplicativo.setUrl_app(result.get("url_app").getAsString());
                    aplicativo.setNm_empresa(result.get("nm_empresa").getAsString());
                    aplicativo.setUrl_logo_splash(result.get("url_logo_splash").getAsString());
                }
            });
    }catch (Exception e){
        Metodos.ShowPopup(context);
    }
}

Could anyone tell me why the object always returns null after the function call?

  • Try to take that reserved word final in the definition of the method and test again

  • Did you try to rescue the attributes before .setCallback() arrow them? Yeah .setCallback() is asynchronous.

1 answer

0


Analyzing your code, the values of the attribute aplicativo will only be available within the public void. I would do the following to use the returned JSON values out of the function:

//Início
Aplicativo dadosApp;
//...
//Sua função
public void preencherObj(Context context, final Aplicativo aplicativo){
    try{
        Ion.with(context)
            .load(Constantes.url)
            .setBodyParameter("id_empresa", String.valueOf(Constantes.id_empresa))
            .setBodyParameter("acao", "B")
            .asJsonObject()
            .setCallback(new FutureCallback<JsonObject>() {
                @Override
                public void onCompleted(Exception e, JsonObject result) {
                    aplicativo.setId_aplicativo(result.get("id_aplicativo").getAsInt());
                    aplicativo.setNm_cor(result.get("nm_cor").getAsString());
                    aplicativo.setNr_telefone(result.get("nr_telefone").getAsString());
                    aplicativo.setNr_telefone2(result.get("nr_telefone2").getAsString());
                    aplicativo.setUrl_logo(result.get("url_logo").getAsString());
                    aplicativo.setUrl_whats(result.get("url_whats").getAsString());
                    aplicativo.setUrl_local(result.get("url_local").getAsString());
                    aplicativo.setUrl_site(result.get("url_site").getAsString());
                    aplicativo.setNm_endereco(result.get("nm_endereco").getAsString());
                    aplicativo.setNm_cidade(result.get("nm_cidade").getAsString());
                    aplicativo.setNm_estado(result.get("nm_estado").getAsString());
                    aplicativo.setCd_cep(result.get("cd_cep").getAsString());
                    aplicativo.setUrl_app(result.get("url_app").getAsString());
                    aplicativo.setNm_empresa(result.get("nm_empresa").getAsString());
                    aplicativo.setUrl_logo_splash(result.get("url_logo_splash").getAsString());
                }
            });
            dadosApp = aplicativo;
    }catch (Exception e){
        Metodos.ShowPopup(context);
    }
}

Thus using the variable dadosApp will have all the information you have in the application internal variable, or if you prefer you can use it in the following way:

//Início
Aplicativo aplicativo;
//...
//Sua função
public void preencherObj(Context context){
    try{
        Ion.with(context)
            .load(Constantes.url)
            .setBodyParameter("id_empresa", String.valueOf(Constantes.id_empresa))
            .setBodyParameter("acao", "B")
            .asJsonObject()
            .setCallback(new FutureCallback<JsonObject>() {
                @Override
                public void onCompleted(Exception e, JsonObject result) {
                    aplicativo.setId_aplicativo(result.get("id_aplicativo").getAsInt());
                    aplicativo.setNm_cor(result.get("nm_cor").getAsString());
                    aplicativo.setNr_telefone(result.get("nr_telefone").getAsString());
                    aplicativo.setNr_telefone2(result.get("nr_telefone2").getAsString());
                    aplicativo.setUrl_logo(result.get("url_logo").getAsString());
                    aplicativo.setUrl_whats(result.get("url_whats").getAsString());
                    aplicativo.setUrl_local(result.get("url_local").getAsString());
                    aplicativo.setUrl_site(result.get("url_site").getAsString());
                    aplicativo.setNm_endereco(result.get("nm_endereco").getAsString());
                    aplicativo.setNm_cidade(result.get("nm_cidade").getAsString());
                    aplicativo.setNm_estado(result.get("nm_estado").getAsString());
                    aplicativo.setCd_cep(result.get("cd_cep").getAsString());
                    aplicativo.setUrl_app(result.get("url_app").getAsString());
                    aplicativo.setNm_empresa(result.get("nm_empresa").getAsString());
                    aplicativo.setUrl_logo_splash(result.get("url_logo_splash").getAsString());
                }
            });
    }catch (Exception e){
        Metodos.ShowPopup(context);
    }
}

This way you ensure that any other function that uses the variable aplicativo you will find the information, because they will be in the global variable and no longer in the local variable of the function preencherObj.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.