Reputation: 3
I have a table called app_catagory
. The problem is, when I try to insert a row into the table, I got this exception:
FATAL EXCEPTION : main java.lang.NullPointerException
The LogCat shows this line as the offending code:
db.insert(DATABASE_TABLE2, null, initialValues);
How can I fix it?
Code
static final String DATABASE_APP_CAT= "create table app_catagory(cat_id interprimary key autoincrement, " +
"name text not null," +
"date_created date , " +
"img_name text);";
//Insert into catagory table
public static void insertContact_app_cat()
{
String[] name={"DOCTOR","PHARMACY","PATHLAB","BLOODBANK"};
for(int i=1;i<=4;i++){
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_CAT_NAME, name[i]);
initialValues1.put(KEY_CAT_DATE, "05-05-2014");
initialValues2.put(KEY_CAT_IMG,"null");
db.insert(DATABASE_TABLE2, null, initialValues);
}
}
Upvotes: 0
Views: 336
Reputation: 168
I don't see where you are getting the db when I do an insert I call this piece of code before doing my ContentValues add stuff.
SQLiteDatabase db = this.getWritableDatabase();
Then after the insert I would close the database.
db.close();
Upvotes: 1