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 (Text):
    1. public List<Agenda> getAllAgenda() {
    2.           List<Agenda> agendas = new ArrayList<Agenda>();
    3.           Cursor cursor;
    4.  
    5.           try {
    6.           cursor = bdd.query(TABLE_AGENDA,
    7.                  new String[] {COL_ID, COL_INTITULE, COL_DATE, COL_HEURE}, null, null, null, null, null);
    8.                 if ( cursor.moveToFirst() ) {
    9.                    while (!cursor.isAfterLast()) {
    10.                       Agenda agenda = cursorToAgenda(cursor);
    11.                       agendas.add(agenda);
    12.                      cursor.moveToNext();
    13.                    }
    14.                 }
    15.                 cursor.close();
    16.           } catch (Exception e){
    17.              Log.d("AGENDA DAO", e.toString() );
    18.           }
    19.  
    20.        return agendas;
    21. }
    For information, I don't call getAllAgenda() in my main activity but in a secondary one... with this code :

    Code (Text):
    1. public class ListeActivity extends ListActivity {
    2.  
    3.    private AgendaDAO agendaDAO;  
    4.     private SimpleCursorAdapter dbAdapter;  
    5.  
    6.     @Override
    7.     public void onCreate(Bundle savedInstanceState) {
    8.         super.onCreate(savedInstanceState);
    9.         setContentView(R.layout.activity_liste);
    10.         ListView listView = (ListView)findViewById(android.R.id.list);
    11.         agendaDAO = new AgendaDAO(this);
    12.        agendaDAO.open();  
    13.         try {
    14.            List<Agenda> agendas = agendaDAO.getAllAgenda();
    15.           Log.d("NB AGENDA",Integer.toString(agendas.size()));
    16.  
    17.            ArrayAdapter<Agenda> adapter = new ArrayAdapter<Agenda>(this,
    18.               android.R.layout.simple_list_item_1, agendas);
    19.           listView.setAdapter(adapter);
    20.         } catch(Exception e){
    21.            Log.d("EXCEPTION",e.toString());
    22.         }
    23.         agendaDAO.close();
    24.     }
    25.  
    26. }
    Any ideas ?

    Thanx a lot !

    Advertisement
    :
  2. Booyakha

    Booyakha New Member

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

    Code (Text):
    1. 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.
Loading...

Share This Page