SQLite and NullPointer in Android
I'd like to use an sqlite database for my android application.First question, do I need to have a rooted device?! If i try adb shell sqlite3 on my Galaxy S, it will return "not found" error. Also, when I try to "ls /data/ " i get "No permission" error.
Second question, the NullPointer itself.
07-23 10:58:17.546: ERROR/AndroidRuntime(12815): Caused by: java.lang.NullPointerException
07-23 10:58:17.546: ERROR/AndroidRuntime(12815): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
07-23 10:58:17.546: ERROR/AndroidRuntime(12815): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:98)
07-23 10:58:17.546: ERROR/AndroidRuntime(12815): at com.gpachov.priorityqueue.database.Database.getAllRecords(Database.java:24)
07-23 10:58:17.546: ERROR/AndroidRuntime(12815): at com.gpachov.priorityqueue.database.DatabaseAdapter.<init>(DatabaseAdapter.java:21)
07-23 10:58:17.546: ERROR/AndroidRuntime(12815): at com.gpachov.priorityqueue.PriorityQueueActivity.<init>(PriorityQueueActivity.java:20)
It happens in the Context itself when trying to access the database.
Here is my code.
public class DatabaseHelper extends SQLiteOpenHelper {
private final static String DATABASE_NAME="queue";
private final static int DATABASE_VERSION = 1;
public DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(ItemTable.CREATE_STRING);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//nothing for now
}
}
And the more important null pointer causing class:
public class Data开发者_开发百科base {
private DatabaseHelper mDatabaseHelper;
public Database(Context context) {
mDatabaseHelper = new DatabaseHelper(context);
}
List<Item> getAllRecords() {
final SQLiteDatabase database = mDatabaseHelper.getReadableDatabase();
.................
It blows with the NPE on the first line of getAllRecords()
.
Oh yeah, and here is my create string:
public static final String CREATE_STRING = "create table items (id integer primary key autoincrement, "+ "title text not null, description text not null, priority integer not null)";
Solved! An obvious and ridiculous mistake, though absolutely impossible to be seen from the text I posted.
I for some reason did instantiate my adapter before the onCreate() statement in the activity. Meaning the context passed below was not null, but not 'ready' yet. Therefore neithter the adapter nor the helper received null-contexts or stuff, but the earlier call with unprepared context results in unset fields and in-depth null-pointers.
Thanks everybody trying to catch this one, but as I did not post activity code, it was virtually incatchable from every other perspective than my own. Sorry about that.
Answer to first question: No, you don't need rooted device.
About: adb shell sqlite3
what are you trying to accomplish here? It does not make sense as shown.
About "No permission": yep, that is normal. You can't access files belonging to app on real device like galaxy s (although it is perfectly possible on emulator).
Second question: obviously mDatabaseHelper
is null
and when you call mDatabaseHelper.getReadableDatabase();
in your getAllRecords()
it causes the NPE. Do you have another constructor for your Database
class? Like one that does not initialize mDatabaseHelper
?
精彩评论