NullPointerException on SQLiteDatabase.query


  1. Booyakha

    Booyakha New Member

    Hi there,

    I got a NullPointerException when I tried to get all the agenda that are recorded in my SQLite database.
    The insert works well, I create a Toast with the ID which is returned, and it increment normally.
    Code:
    public List<Agenda> getAllAgenda() {
              List<Agenda> agendas = new ArrayList<Agenda>();
              Cursor cursor;
     
              try {
              cursor = bdd.query(TABLE_AGENDA,
                     new String[] {COL_ID, COL_INTITULE, COL_DATE, COL_HEURE}, null, null, null, null, null);
                    if ( cursor.moveToFirst() ) {
                       while (!cursor.isAfterLast()) {
                          Agenda agenda = cursorToAgenda(cursor);
                          agendas.add(agenda);
                         cursor.moveToNext();
                       }
                    }
                    cursor.close();
              } catch (Exception e){
                 Log.d("AGENDA DAO", e.toString() );
              }
     
           return agendas;
    }
    For information, I don't call getAllAgenda() in my main activity but in a secondary one... with this code :

    Code:
    public class ListeActivity extends ListActivity {
     
       private AgendaDAO agendaDAO;  
        private SimpleCursorAdapter dbAdapter;  
     
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_liste);
            ListView listView = (ListView)findViewById(android.R.id.list);
            agendaDAO = new AgendaDAO(this);
           agendaDAO.open();   
            try {
               List<Agenda> agendas = agendaDAO.getAllAgenda();
              Log.d("NB AGENDA",Integer.toString(agendas.size()));
     
               ArrayAdapter<Agenda> adapter = new ArrayAdapter<Agenda>(this,
                  android.R.layout.simple_list_item_1, agendas);
              listView.setAdapter(adapter);
            } catch(Exception e){
               Log.d("EXCEPTION",e.toString());
            }
            agendaDAO.close();
        }
     
    }
    Any ideas ?

    Thanx a lot !

    Advertisement
  2. Booyakha

    Booyakha New Member

    Sorry the Exception isn't a NullPointer but this one :

    Code:
    java.lang.IllegalStateException: attempt to re-open an  already-closed object: android.database.sqlite.SQLiteQuery (mSql =  SELECT ID, Intitul
  3. jonbonazza

    jonbonazza Well-Known Member

    Once a database is closed, a new instance needs to be created in order to reopen it. You can't just re open a closed database. Another option is to just leave the data base open until the app is shut down.

Share This Page