0

I create this table:

"create table "  + DATABASE_TABLE + " ("
    + KEY_ROWID + " integer primary key autoincrement not null, "
    + KEY_TITLE + " text not null, "
    + KEY_BODY + " text not null, " + KEY_DATE_TIME + " text not null);"

and here is what I do for inserting:

ContentValues initialValues = new ContentValues();
initialValues.put("title", title);
initialValues.put("body", body);
initialValues.put("abc", anc);
return mDb.insert(DATABASE_TABLE, null, initialValues); 

but due to some error my app force closes. Help me out please...

Logcat errors:

java.lang.NullPointerException
com.maddy.task_reminder.ReminderDbAdapter.createReminder(ReminderDbAdapter.java:133)
06-09 23:28:44.112: E/AndroidRuntime(412):  at com.maddy.task_reminder.edit_activity$4.onClick(edit_activity.java:143)
Sam
  • 86,580
  • 20
  • 181
  • 179
maddy
  • 216
  • 4
  • 17

2 Answers2

0

If you look at java.lang.NullPointerException com.maddy.task_reminder.ReminderDbAdapter.createReminder(ReminderDbAdapter.java:133) 06-09 23:28:44.112: E/AndroidRuntime(412): at com.maddy.task_reminder.edit_activity$4.onClick(edit_activity.java:143)

You have errors at edit_activity.java:143 and ReminderDbAdapter.java:133. Whats there?

And my code for the same thing looks like this

    `public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_NAME + " TEXT NOT NULL, " +
                KEY_REL + " TEXT NOT NULL);"
        );
    }`

and

public long createEntry(String name, String rel) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME, name);
    cv.put(KEY_REL, rel);
    return water.insert(DATABASE_TABLE, null, cv);
}

and it works fine.

  • in edit_activity...... on line mDbHelper.createReminder(title, body, reminderDateTime); and in reminderdbadaptor on line return mDb.insert(DATABASE_TABLE, null, initialValues); – maddy Jun 09 '12 at 18:23
  • @webstreet The easiest way to add a block of code here in SO is to highlight the code and press `+k`. – Sam Jun 09 '12 at 18:24
  • The author is performing an insertion not an update, he won't need the row id. – Sam Jun 09 '12 at 18:28
  • yea sql is not case sensitive – maddy Jun 09 '12 at 18:31
  • Check out my project with sort of the same thing... https://github.com/jordanbtahabsim/Family-Tree/tree/master/src/com/webstreetlabs/FamilyTree –  Jun 09 '12 at 18:35
0

If you are following the generic DBAdapter examples on the web, check that you call:

mDbHelper.open();

in your Activity. Otherwise mDb is never instantiated in your database helper class, it is always null.

Addition from Comment:

So this is your open() method.

public ReminderDbAdapter open() throws SQLException { 
    mDbHelper = new DatabaseHelper(mCtx); 
    mDb = mDbHelper.getWritableDatabase(); 
    return this; 
}

If your ReminderDbAdapter constructor doesn't look like this:

public ReminderDbAdapter(Context context) {
    ...
    mCtx = context;
    open(); // Do you have this line?
}

Then you need to explicitly call ReminderDbAdapter.open() in your activity, possibly like this:

ReminderDbAdapter mDbHelper = new ReminderDbAdapter(this); 
mDbHelper.open();
Sam
  • 86,580
  • 20
  • 181
  • 179
  • The safest place is immediately after creating it. `DbHelper mDbHelper = new DbHelper(this); mDbHelper.open();` – Sam Jun 09 '12 at 18:39
  • dont you think contructor will open the db helper as code shows below....: public ReminderDbAdapter open() throws SQLException { mDbHelper = new DatabaseHelper(mCtx); mDb = mDbHelper.getWritableDatabase(); return this; } – maddy Jun 09 '12 at 18:56
  • ya as you can see constructor is opening database in writing mode – maddy Jun 09 '12 at 19:15
  • I don't see your constructor anywhere. `public ReminderDbAdapter open()` is a method of the adapter class **not** the constructor. – Sam Jun 09 '12 at 19:17
  • i cant chat cozz of reputation thing and okk letme try this thing calling open – maddy Jun 09 '12 at 19:19
  • thank you....:)) error removed...i am sort of new to android and thanx alot for helping sam.....:)) – maddy Jun 09 '12 at 19:27
  • and can you please tell me the usage of context i am kind of confused about it..... – maddy Jun 09 '12 at 19:29
  • Glad you are making progress, good luck! Please mark the answer as solved by clicking the checkmark. And this should help understand Context a little better: [What is Context in Android?](http://stackoverflow.com/q/3572463/1267661) – Sam Jun 09 '12 at 19:34