Skip to content Skip to sidebar Skip to footer

How To Do I Resolve Illegalargumentexception?

I'm a noob to android. and I am getting an illegal argument exception when working with a SQLite table. The logcat indicated that this was due to my cursor not being closed so i c

Solution 1:

Try changing the DATABASE_VERSION value to 2 and increment when you change the table structure, add columns or add tables in the database class.

The change in the DATABASE_VERSION triggers the call to onUpgrade() which in your case will drop the table and create a new one. As long as the DATABASE_VERSION is the same, onUpgrade() or onCreate() won't be called therefore the structure won't be affected and therefore not updating the database structure. This could be the reason why you are getting the IllegalArgumentException as it probably can't find a column in the device's database because the column isn't there. This is what the documentation means when it says:

Note: this class assumes monotonically increasing version numbers for upgrades.

http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html

Thus, if you make a simple database, run it, then add a column in the onCreate() method without changing the database version, the database class methods onCreate() or the onUpgrade() won't be called, because as far as it's concerned, the database is the same.

You must remember that an Android local database is persistent. It will stay on your device beyond the app running. Thus the database holds information from one run of the app to another. The database does not reset every time you run the application. This not only includes the data it holds but also the database structure, such as tables and columns.

When you run the code first is that it will check if a database has been created already. This is on the actual device. If it exists it will check if the database version on the device matches the DATABASE_VERSION in the helper class. If it does, it doesn't do anything. If the DATABASE_VERSION in the database class is greater than the database version on the device, then the onUpgrade() method is called and will adjust the database as defined.

Solution 2:

Make sure you leave no pending transactions and open cursors.

For INSERT, UPDATE, DELETE:

db.beginTransaction();
try{

    //INSERT/UPDATE/DELETE statements here

    db.setTransactionSuccessful();
}finally {
    db.endTransaction();
}

For Cursors:

Cursorc= db.query( // query codetry{
   while(c.moveToNext()){

     //  read data

   }
} finally{
   c.close();
}

Solution 3:

Try renaming the database name and table names to all lowercase.

privatestaticfinalStringDATABASE_NAME="portfolio_database";
privatestaticfinalStringDATABASE_TABLE="cointype_table";

Has the database worked before?

Post a Comment for "How To Do I Resolve Illegalargumentexception?"