views:

34

answers:

1

Hi all, currently I'm working on my 1st Android app and I encounter something I cannot solve. When I try to insert data into my own database there is an error: 07-18 03:41:04.414: ERROR/AndroidRuntime(3480): java.lang.NullPointerException

My code for inserting into table is:

public long createRecord(String created_time, String modified_time,
        long reading, String unit, String period, String remark) {

    ContentValues values = new ContentValues(6);
    values.put(KEY_CREATED_TIME, created_time);
    values.put(KEY_MODIFIED_TIME, modified_time); //System.out.println(values);
    values.put(KEY_RECORD, reading); //System.out.println(values);
    values.put(KEY_UNIT, unit); //System.out.println(values);
    values.put(KEY_PERIOD, period);// System.out.println(values);
    values.put(KEY_REMARK, remark); 

    System.out.println(values);
    return mDb.insert(DATABASE_TABLE, null , values);

}

This is under my Adapter class which is an instance of my DatabaseHelper class. For my DatabaseHelper class, I can copy created database from assets folder into system databases folder. But just cannot create data into table, I tried to print out the "values", it showed that:

created_time=2010-07-18 03:41:04 remark=on unit=mg/dL reading=67 period=Before Meal modified_time=00:00:00 which was not the way it supposed to be, so I guessed this might be the problem. Can anyone help me with this? Searched online but not so many info on this. Thanks in advance:)

A: 

The reason for the order not being retained is that ContentValues is backed by a HashMap. So that's ok.
The problem is likely caused by mDb being null (check it with System.out.println("" + mDb);).

alex
Ya, I've checked mDb it showed "null". May I know the reason for this and how can I fix it? Thanks
Wen
Something went wrong on the line where you get a reference: `SQLiteDatabase mDb = // null here;`
alex
Ok, I added mDb = mDbHelper.getWritableDatabase(); Now, the code works fine in Emulator mode but not on phone. If I run the code on device it shows an error msg: 07-18 13:51:49.595: ERROR/Database(2022): Error inserting remark=ol period=Before Meal modified_time=00:00:00 unit=mg/dL reading=68 created_time=2010-07-18 13:51:4907-18 13:51:49.595: ERROR/Database(2022): android.database.sqlite.SQLiteException: no such table: records: , while compiling: INSERT INTO records(remark, period, modified_time, unit, reading, created_time) VALUES(?, ?, ?, ?, ?, ?);Any advice?
Wen
Read the exception.
alex
You mean no such table? But I run adb shell the table is there for emulator. As for device it always give me opendir failed, permission denied. Is there any diff between database in system and device? Quite confused by this.
Wen
No difference. You haven't created the table.
alex
Thanks for your quick reply:) If I have not created the table why Emulator can save data in the said table? I created my own database with tables: android.metadata and records using SQLite Database Browser. Saved the database file in my project's assets folder. In my Databasehelper class, I copied this database to /data/data/com.xx.xxx/databases/database. Do I need to create the table again in my code? And under device run, can I view the adb shell for the tables?
Wen
I'd suggest you read about `SQLiteOpenHelper` and use it.
alex
Yeh, I uninstalled my application on phone and run it again, the problem got solved finally :)
Wen