java.lang.Nullpointerexception error when trying to popular Spinner

Asked

Viewed 37 times

0

Inside the Oncreate:

  spnMateriasID = (Spinner) findViewById(R.id.spnMateria);


        try{

            atividadeModel = new AtividadeModel(this);
            atividadeModel.getWritableDatabase();

            materiaModel = new MateriaModel(this);
            materiaModel.getWritableDatabase();

            adapterMateria = buscaMaterias(this);

        } catch(SQLException ex){
            Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
        }


        spnMateriasID.setAdapter(adapterMateria);




     public ArrayAdapter<String> buscaMaterias(Context context){
        ArrayAdapter<String> materias = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);

        Cursor dados = conn.query("materias",null, null, null, null, null, null);

        if(dados.getCount() > 0){
            dados.moveToFirst();

            while(dados.moveToNext()){
                materias.add(dados.getString(1));
            }

        }

        return materias;
    }
  • Felipe puts more details of the problem in the question. On which line exactly occurs the error?

  • This is the error generated when trying to fetch the data and popular the Arrayadapter; Problem is that every time I try to open the connection of some error.

  • 1

    Where the variable is defined conn? It seems that it lacks more codes there, puts the error stack tbm.

  • Caused by: java.lang.Nullpointerexception at com.example.Felipe.tutor.Additionactivity.searches(Additionactivity.java:120) at with.example.Felipe.tutor.Additionactivity.onCreate(Additionactivity.java:61) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) Could Not Do It All

  • Above Oncreate I have declared: private Sqlitedatabase Conn;

  • 1

    You initialized the variable conn? Assign an instance to it?

  • Our lack of attention. I started Sqlitedatabase Conn and it worked, Thank you so much for helping me out.

Show 2 more comments
No answers

Browser other questions tagged

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