Why does the getter return null?

Asked

Viewed 104 times

0

In my android studio project I have a class of getters and setters to help me, but for some reason on main Activity when I call the db.busca() (which is in the database operations class) which places a token in the u.setToken("comes from comic book").

Then on Mainactivity if I give user.getToken() it comes null, I’ve had to test if I give a user.getToken() in the Database class everything works ok, but in Mainactivity results null.

In some cases I want it to return null but when it should not return null and yes a token it returns null

Class of the Database:

    public void busca(){
    String[] colunas = new String[]{"token", "nome"};
    Cursor cursor = db.query("user", colunas, null, null, null, null, "token ASC");
    if(cursor.getCount() > 0){
        cursor.moveToFirst();
        do{
            Utilizador u = new Utilizador();
            u.setToken(cursor.getString(0));
            u.setNome(cursor.getString(1));
        }while(cursor.moveToNext());
    }
}

User class:

package imm.pt.immflix;

    public class Utilizador {
        private String nome;
        private String mail;
        private String token;
        private String foto;
        private int typeService;

        public String getNome() {
            return nome;
        }

        public void setNome(String nome) {
            this.nome = nome;
        }

        public String getMail() {
            return mail;
        }

        public void setMail(String mail) {
            this.mail = mail;
        }

        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token;
        }

        public String getFoto() {
            return foto;
        }

        public void setFoto(String foto) {
            this.foto = foto;
        }

        public int getTypeService() {
            return typeService;
        }

        public void setTypeService(int typeService) {
            this.typeService = typeService;
        }
    }

Mainactivity class:

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            getSupportActionBar().hide();
            TextView tvtitle = findViewById(R.id.tvTitle);
            ObjectAnimator anime = ObjectAnimator.ofFloat(tvtitle, "alpha", 0f, 1f);
            anime.setDuration(2000);
            anime.start();
            DB db = new DB(this);
            db.busca();
            Utilizador user = new Utilizador();
            token = user.getToken();
            if(token == null){
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent(MainActivity.this, Login.class);
                        intent.putExtra("isNew", true);
                        startActivity(intent);
                        finish();
                    }
                }, 3000);
            }else{
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent(MainActivity.this, Login.class);
                        intent.putExtra("isNew", false);
                        startActivity(intent);
                        finish();
                    }
                }, 3000);
            }

        }

1 answer

1


The user.getToken() is being used right after object instantiation user.

Utilizador user = new Utilizador();
token = user.getToken();

As no value has been assigned to the field token user.getToken() returns null.

With the elements made available I believe that the method busca() should return a User to be used in the Activity class:

public Utilizador busca(){
    String[] colunas = new String[]{"token", "nome"};
    Cursor cursor = db.query("user", colunas, null, null, null, null, "token ASC");
    if(cursor.getCount() > 0){
        cursor.moveToFirst();
        do{
            Utilizador u = new Utilizador();
            u.setToken(cursor.getString(0));
            u.setNome(cursor.getString(1));
        }while(cursor.moveToNext());
        return u;
    }
    return null;
}

The code in Activity should be changed to:

DB db = new DB(this);
Utilizador user = db.busca();

Since there is the possibility of the query returning nothing, add a test to verify this.

I don’t know if the query can return more than one User or not, if it returns, the method should return a User list.

I wasn’t looking for anything else "wrong".

  • Thank you, that was the problem, and yes only return a "User" technically returns a token and the name of the user logged into my APP and this token is then checked with the server to perform other actions, thank you again!

Browser other questions tagged

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