开发者

Android SQLiteDatabase Null Pointer Exception

I've just started developing for Android. So far it's been surprisingly smooth sailing, but now I've run into some problems implementing an SQLite database. My code is based on Google's Notepad example (which runs perfectly well), but I keep getting Null Pointer Exceptions thrown at me an开发者_开发百科d it seems to be related to the getWritableDatabase()-method.

The offending class is 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;
   }
}

Whenever I try to open the database using

QuizDbHelper databaseHelper = new QuizDbHelper(this);
databaseHelper.open(); // <-- X(

the application chokes and serves up a Null Pointer Exception indicating the call to open().

Stack trace:

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


Is Quiz.newQuiz a static method? If that is the case, this is not available (and as such a null pointer).


Make sure the database is opened properly like you can create some Adapter Class, which you can call it whenever you want

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜