Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

Get excited for the Samsung Galaxy S5! Find everything you need and discuss it in our Galaxy S5 Forum!

test: Reply
 
LinkBack Thread Tools
Old November 14th, 2012, 03:22 AM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: Nov 2012
Posts: 2
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Question NullPointerException on SQLiteDatabase.query

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 !

Booyakha is offline  
Reply With Quote
sponsored links
Old November 14th, 2012, 07:22 AM   #2 (permalink)
New Member
Thread Author (OP)
 
Join Date: Nov 2012
Posts: 2
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

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é, Date, Heure FROM agenda)
Booyakha is offline  
Reply With Quote
Old November 14th, 2012, 02:58 PM   #3 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,936
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 478 Times in 292 Posts
Default

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.
__________________
My blog, Inverted Keystrokes, is looking for aspiring developers (not necessarily with Android) to post articles. If you have any development experience and are interested in participating, please PM me. =)
jonbonazza is offline  
Reply With Quote
Reply
Tags
nullpointerexception, sqlitedatabase


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 10:42 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.