Android上的SQLiteOpenHelper.getWriteableDatabase()空指针exception

SQLite中,我使用了直接的SQL直接使用SQLite,但是这是我第一次将数据库封装到ContentProvider中。 调用getWritableDatabase()getReadableDatabase()时,我总是收到空指针exception。 这只是我在代码中初始化时犯的一个愚蠢的错误,还是有更大的问题?

 public class DatabaseProvider extends ContentProvider { ... private DatabaseHelper databaseHelper; private SQLiteDatabase db; ... @Override public boolean onCreate() { databaseHelper = new DatabaseProvider.DatabaseHelper(getContext()); return (databaseHelper == null) ? false : true; } ... @Override public Uri insert(Uri uri, ContentValues values) { db = databaseHelper.getWritableDatabase(); // NULL POINTER EXCEPTION HERE ... } private static class DatabaseHelper extends SQLiteOpenHelper { public static final String DATABASE_NAME = "cogsurv.db"; public static final int DATABASE_VERSION = 1; public static final String[] TABLES = { "people", "travel_logs", "travel_fixes", "landmarks", "landmark_visits", "direction_distance_estimates" }; // people._id does not AUTOINCREMENT, because it's set based on server's people.id public static final String[] CREATE_TABLE_SQL = { "CREATE TABLE people (_id INTEGER PRIMARY KEY," + "server_id INTEGER," + "name VARCHAR(255)," + "email_address VARCHAR(255))", "CREATE TABLE travel_logs (_id INTEGER PRIMARY KEY AUTOINCREMENT," + "server_id INTEGER," + "person_local_id INTEGER," + "person_server_id INTEGER," + "start DATE," + "stop DATE," + "type VARCHAR(15)," + "uploaded VARCHAR(1))", "CREATE TABLE travel_fixes (_id INTEGER PRIMARY KEY AUTOINCREMENT," + "datetime DATE, " + "latitude DOUBLE, " + "longitude DOUBLE, " + "altitude DOUBLE," + "speed DOUBLE," + "accuracy DOUBLE," + "travel_mode VARCHAR(50), " + "person_local_id INTEGER," + "person_server_id INTEGER," + "travel_log_local_id INTEGER," + "travel_log_server_id INTEGER," + "uploaded VARCHAR(1))", "CREATE TABLE landmarks (_id INTEGER PRIMARY KEY AUTOINCREMENT," + "server_id INTEGER," + "name VARCHAR(150)," + "latitude DOUBLE," + "longitude DOUBLE," + "person_local_id INTEGER," + "person_server_id INTEGER," + "uploaded VARCHAR(1))", "CREATE TABLE landmark_visits (_id INTEGER PRIMARY KEY AUTOINCREMENT," + "server_id INTEGER," + "person_local_id INTEGER," + "person_server_id INTEGER," + "landmark_local_id INTEGER," + "landmark_server_id INTEGER," + "travel_log_local_id INTEGER," + "travel_log_server_id INTEGER," + "datetime DATE," + "number_of_questions_asked INTEGER," + "uploaded VARCHAR(1))", "CREATE TABLE direction_distance_estimates (_id INTEGER PRIMARY KEY AUTOINCREMENT," + "server_id INTEGER," + "person_local_id INTEGER," + "person_server_id INTEGER," + "travel_log_local_id INTEGER," + "travel_log_server_id INTEGER," + "landmark_visit_local_id INTEGER," + "landmark_visit_server_id INTEGER," + "start_landmark_local_id INTEGER," + "start_landmark_server_id INTEGER," + "target_landmark_local_id INTEGER," + "target_landmark_server_id INTEGER," + "datetime DATE," + "direction_estimate DOUBLE," + "distance_estimate DOUBLE," + "uploaded VARCHAR(1))" }; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); Log.v(Constants.TAG, "DatabaseHelper()"); } @Override public void onCreate(SQLiteDatabase db) { Log.v(Constants.TAG, "DatabaseHelper.onCreate() starting"); // create the tables int length = CREATE_TABLE_SQL.length; for (int i = 0; i < length; i++) { db.execSQL(CREATE_TABLE_SQL[i]); } Log.v(Constants.TAG, "DatabaseHelper.onCreate() finished"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { for (String tableName : TABLES) { db.execSQL("DROP TABLE IF EXISTS" + tableName); } onCreate(db); } } } 

一如既往,感谢您的帮助!

不知道这个细节是否有帮助,但这里是LogCat显示exception:

删除了死的ImageShack链接

Solutions Collecting From Web of "Android上的SQLiteOpenHelper.getWriteableDatabase()空指针exception"

而不是:

 databaseHelper = new DatabaseProvider.DatabaseHelper(getContext()); 

尝试:

 databaseHelper = new DatabaseProvider.DatabaseHelper(this); 

看看是否有帮助

如果没有,你可以发布NullPointerException堆栈跟踪?

这个错误几乎可以肯定是由于传递了一个无效的上下文:

 super(context, DATABASE_NAME, null, DATABASE_VERSION); 

我只是有一个非活动的SQLite nullpointerexception这个问题,问题是它有一个无效的上下文。

我的修复是在onCreate而不是构造函数传递上下文。 尝试从另一个地方find一个上下文来执行getApplicationContext()

尝试使用getWritableDatabase()在初始化你的帮助器之后,你得到了NullPointerException导致数据库没有完全准备好,不要在ui线程中这样做,否则可能会导致ANR

我只是有这个问题,并意识到这是一个红鲱鱼。

我的问题源于我使用ContentProvider的方式。

我实际上是直接访问ContentProvider,而不是通过ContentResolver。 如果你使用ContentProviders的话,这本质上是一个简单的方法。

希望帮助:)

确保在创buildAVD时,指定一定数量的SD卡空间。 我有同样的问题,这解决了它。 如果这没有帮助,您也可以尝试使用-wipe-data选项启动模拟器。

getReadableDatabase()在query()中被调用时,我得到了相同的NullPointerException错误,因为我不正确地查询我的内容提供者。 我看不出你构build数据库的方式有什么问题。 getContext()应该给你适当的上下文对象。 但是,我会注意你如何向内容提供者提出查询。 你应该确保你有一个有效的引用你的内容parsing器(即通过调用getContentResolver()。query(..))并使用它来执行查询。