getWritableDatabase()抛出空指针异常

时间:2012-11-01 18:21:59

标签: android sqlite

我的服务类有以下代码 -

public class ReminderService extends WakeReminderIntentService{      
    private static final String TAG ="ReminderService";
    private TasksDBAdapter dbHelper;
    public ReminderService(){
        super("ReminderService");
        dbHelper = new TasksDBAdapter(this);
        dbHelper.open(); //THIS LINE GIVES ERROR
    }
TasksDBAdapter open()方法的

代码为 -

    public TasksDBAdapter(Context ctx) {                                 
       this.mCtx = ctx;
   }

    public TasksDBAdapter open() throws android.database.SQLException {
           mDbHelper = new DatabaseHelper(mCtx);
           Log.i(TAG, "mDbHelper in open method-->" + mDbHelper);
           mDb = mDbHelper.getWritableDatabase(); // ACTUAL ERROR LINE
           return this;
       }

日志值接收地址,这意味着mDbHelper正确初始化.Log.i给出 -

11-01 23:32:32.004: I/TasksDBAdapter(681): mDbHelper in open method-->ray.kaushik.simple.task.manager.TasksDBAdapter$DatabaseHelper@411e7a10

但是

mDb = mDbHelper.getWritableDatabase();

抛出空指针错误。

Logcat给出 -

11-01 23:32:32.043: E/AndroidRuntime(681): FATAL EXCEPTION: main
11-01 23:32:32.043: E/AndroidRuntime(681): java.lang.RuntimeException: Unable to instantiate service ray.kaushik.simple.task.manager.ReminderService: java.lang.NullPointerException
11-01 23:32:32.043: E/AndroidRuntime(681):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:2347)
11-01 23:32:32.043: E/AndroidRuntime(681):  at android.app.ActivityThread.access$1600(ActivityThread.java:130)
11-01 23:32:32.043: E/AndroidRuntime(681):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
11-01 23:32:32.043: E/AndroidRuntime(681):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 23:32:32.043: E/AndroidRuntime(681):  at android.os.Looper.loop(Looper.java:137)
11-01 23:32:32.043: E/AndroidRuntime(681):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-01 23:32:32.043: E/AndroidRuntime(681):  at java.lang.reflect.Method.invokeNative(Native Method)
11-01 23:32:32.043: E/AndroidRuntime(681):  at java.lang.reflect.Method.invoke(Method.java:511)
11-01 23:32:32.043: E/AndroidRuntime(681):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-01 23:32:32.043: E/AndroidRuntime(681):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-01 23:32:32.043: E/AndroidRuntime(681):  at dalvik.system.NativeStart.main(Native Method)
11-01 23:32:32.043: E/AndroidRuntime(681): Caused by: java.lang.NullPointerException
11-01 23:32:32.043: E/AndroidRuntime(681):  at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
11-01 23:32:32.043: E/AndroidRuntime(681):  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
11-01 23:32:32.043: E/AndroidRuntime(681):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
11-01 23:32:32.043: E/AndroidRuntime(681):  at ray.kaushik.simple.task.manager.TasksDBAdapter.open(TasksDBAdapter.java:38)
11-01 23:32:32.043: E/AndroidRuntime(681):  at ray.kaushik.simple.task.manager.ReminderService.<init>(ReminderService.java:16)
11-01 23:32:32.043: E/AndroidRuntime(681):  at java.lang.Class.newInstanceImpl(Native Method)
11-01 23:32:32.043: E/AndroidRuntime(681):  at java.lang.Class.newInstance(Class.java:1319)
11-01 23:32:32.043: E/AndroidRuntime(681):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:2344)
11-01 23:32:32.043: E/AndroidRuntime(681):  ... 10 more

我的databaseHelper类代码 -

    private static class DatabaseHelper extends SQLiteOpenHelper {          
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);      
        }
        @Override
        public void onCreate(SQLiteDatabase db) {                       
            db.execSQL(DATABASE_CREATE);                                
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {                                   

        }
    }

其中DATABASE_NAME和所有其他人已全球定义 -

private static final String DATABASE_NAME = "data";              
    private static final String DATABASE_TABLE = "reminders";        
    private static final int DATABASE_VERSION = 1;                   

    public static final String KEY_TITLE = "title";                  
    public static final String KEY_BODY = "body";
    public static final String KEY_DATE_TIME = "reminder_date_time"; 
    public static final String KEY_ROWID = "_id";                    
    private static final String TAG = "TasksDBAdapter";

任何人都可以提供一些指示..提前感谢.. !!!

3 个答案:

答案 0 :(得分:0)

我相信当您尝试在构造函数中创建数据库时,IntentService尚未完全初始化。尝试将代码移到onHandleIntent()

protected void onHandleIntent(Intent intent) {
    dbHelper = new TasksDBAdapter(this);
    dbHelper.open();
    // Do something
}

答案 1 :(得分:0)

我有点解决了这个问题 -

我有ReminderService类,我试图调用dbHelper.open();方法

此同一个类还有一个重写方法doReminderWork(intent),我将使用dbHelper初始化。

早期代码 -

  public ReminderService(){
            super("ReminderService");
            dbHelper = new TasksDBAdapter(this);
            dbHelper.open(); //THIS LINE GIVES ERROR
        }

    @Override
        void doReminderWork(Intent intent){ 

    /**
              * retrieve data about task
              */

             Cursor reminder = dbHelper.fetchReminder(rowId);
   }

我在doReminderWork(Intent intent)方法中移动了open方法,如 -

public ReminderService(){
                super("ReminderService");
                dbHelper = new TasksDBAdapter(this);

            }

        @Override
            void doReminderWork(Intent intent){ 

        /**
                  * retrieve data about task
                  */
                dbHelper.open(); //MOVED HERE
                 Cursor reminder = dbHelper.fetchReminder(rowId);
       }

并且它有效..任何人都可以给我正确的详细解释为什么这有效,因为它只是一个有用的尝试。

答案 2 :(得分:0)

必须从单独的线程调用此类操作,尝试使用Thread对象或AsyncTask调用它。

Thread t = new Thread(new Runnable() { 
    public void run() {
        dbHelper.open(); //MOVED HERE
        Cursor reminder = dbHelper.fetchReminder(rowId);
    }
});
t.start();