Android SQLiteDatabase空指针异常

时间:2010-10-06 20:28:27

标签: android sqlite

我刚刚开始为Android开发。到目前为止,它出乎意料地顺利航行,但现在我遇到了一些实现SQLite数据库的问题。我的代码基于Google的记事本示例(运行得非常好),但我不断向我发出空指针异常,它似乎与getWritableDatabase()方法有关。

违规类是QuizDbAdapter:

package com.mypackage.myapp;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class QuizDbAdapter {
   public static final String KEY_ROWID =         "_id";
   public static final String KEY_QUESTION =      "question";
   public static final String KEY_ANSWER1 =      "answer1";
   public static final String KEY_ANSWER2 =      "answer2";
   public static final String KEY_ANSWER3 =      "answer3";
   public static final String KEY_ANSWER4 =      "answer4";
   public static final String KEY_CORRECT_ANSWER =   "correct_answer";
   public static final String KEY_IMAGE =         "image";
   public static final String KEY_CATEGORIES =      "categories";
   public static final String KEY_IMAGEQUESTION =   "image_question";
   public static final String KEY_EXPLANATION =   "explanation";

   private static final String TAG = "QuizDbAdapter";
   private DatabaseHelper databaseHelper;
   private SQLiteDatabase database;

   private static final String DATABASE_CREATE =
      "create table questions (_id integer primary key autoincrement, "
        + "question text not null, answer1 text not null, answer2 text not null,"
        + "answer3 text not null, answer4 text not null, correct_answer integer not null,"
        + "image text not null, categories text not null, image_question boolean not null,"
        + "explanation text not null);"
        + "create table roadsigns (_id integer primary key autoincrement, "
        + ");";

   private static final String DATABASE_NAME = "mydb.sqlite";
   private static final int DATABASE_VERSION = 1;
   private static final String TABLE_QUESTIONS = "questions";

   private final Context context;

   private static class DatabaseHelper extends SQLiteOpenHelper {
      DatabaseHelper(Context c) {
         super(c, 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) {
         db.execSQL("DROP TABLE IF EXISTS questions");
            onCreate(db);
      }
   }

   public QuizDbAdapter(Context c) {
      this.context = c;
   }

   public QuizDbAdapter open() throws SQLException {
      databaseHelper = new DatabaseHelper(context);
      database = databaseHelper.getWritableDatabase();
      return this;
   }

   public void close() {
      databaseHelper.close();
   }

   public Cursor fetchAllQuestions() {
      return database.query(TABLE_QUESTIONS, new String[] { KEY_ROWID, KEY_QUESTION,
            KEY_ANSWER1, KEY_ANSWER2, KEY_ANSWER3, KEY_ANSWER4, KEY_CORRECT_ANSWER,
            KEY_IMAGE, KEY_CATEGORIES, KEY_IMAGEQUESTION, KEY_EXPLANATION },
            null, null, null, null, null);
   }

   public Cursor fetchQuestion(long rowId) throws SQLException {
      Cursor cursor = database.query(true, TABLE_QUESTIONS, new String[] { KEY_ROWID,
            KEY_QUESTION, KEY_ANSWER1, KEY_ANSWER2, KEY_ANSWER3, KEY_ANSWER4,
            KEY_CORRECT_ANSWER, KEY_IMAGE, KEY_CATEGORIES, KEY_IMAGEQUESTION,
            KEY_EXPLANATION }, KEY_ROWID + "=" + rowId, null, null, null, null, null);
      if (cursor != null)
         cursor.moveToFirst();
      return cursor;
   }
}

每当我尝试使用

打开数据库时
QuizDbHelper databaseHelper = new QuizDbHelper(this);
databaseHelper.open(); // <-- X(

应用程序阻塞并提供一个空指针异常,指示对open()的调用。

堆栈追踪:

10-06 23:24:02.104: ERROR/AndroidRuntime(369): FATAL EXCEPTION: main
10-06 23:24:02.104: ERROR/AndroidRuntime(369): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mypackage.myapp/com.mypackage.myapp.QuestionActivity}: java.lang.NullPointerException
10-06 23:24:02.104: ERROR/AndroidRuntime(369):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-06 23:24:02.104: ERROR/AndroidRuntime(369):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-06 23:24:02.104: ERROR/AndroidRuntime(369):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-06 23:24:02.104: ERROR/AndroidRuntime(369):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-06 23:24:02.104: ERROR/AndroidRuntime(369):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 23:24:02.104: ERROR/AndroidRuntime(369):     at android.os.Looper.loop(Looper.java:123)
10-06 23:24:02.104: ERROR/AndroidRuntime(369):     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-06 23:24:02.104: ERROR/AndroidRuntime(369):     at java.lang.reflect.Method.invokeNative(Native Method)
10-06 23:24:02.104: ERROR/AndroidRuntime(369):     at java.lang.reflect.Method.invoke(Method.java:521)
10-06 23:24:02.104: ERROR/AndroidRuntime(369):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-06 23:24:02.104: ERROR/AndroidRuntime(369):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-06 23:24:02.104: ERROR/AndroidRuntime(369):     at dalvik.system.NativeStart.main(Native Method)
10-06 23:24:02.104: ERROR/AndroidRuntime(369): Caused by: java.lang.NullPointerException
10-06 23:24:02.104: ERROR/AndroidRuntime(369):     at com.mypackage.myapp.Quiz.newQuiz(Quiz.java:88)
10-06 23:24:02.104: ERROR/AndroidRuntime(369):     at com.mypackage.myapp.QuestionActivity.onCreate(QuestionActivity.java:21)
10-06 23:24:02.104: ERROR/AndroidRuntime(369):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-06 23:24:02.104: ERROR/AndroidRuntime(369):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-06 23:24:02.104: ERROR/AndroidRuntime(369):     ... 11 more

2 个答案:

答案 0 :(得分:1)

Quiz.newQuiz是静态方法吗?如果是这种情况,则this不可用(并且就像这样的空指针)。

答案 1 :(得分:0)

确保正确打开数据库,就像您可以创建一些适配器类,您可以随时调用它

public SupplierAdapter open() throws SQLException {
        dbHelper = new DatabaseHelper(context);
        database = dbHelper.getWritableDatabase();
        return this;
    }