SQLite and Null Pointer Exception, help please!

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

SQLite and Null Pointer Exception, help please!

Postby royen » Wed Oct 06, 2010 7:39 pm


I'm just getting started with Android development coming from about a year's experience developing applications for the iPhone. I'm working on porting a database-driven app from Objective-C/iOS to Java/Android and the development has been surprisingly smooth so far, but now I've run into some trouble, specifically with the SQLite-classes throwing Null Pointer Exceptions at me thus:

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

My QuizDbAdapter, very much adapted from the Google's Notepad example (the Notepad example, by the way, works when I run it).
Code: Select all
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);
      public void onCreate(SQLiteDatabase db) {
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
         db.execSQL("DROP TABLE IF EXISTS questions");
   public QuizDbAdapter(Context c) {
      this.context = c;
   public QuizDbAdapter open() throws SQLException {
      databaseHelper = new DatabaseHelper(context);
      database = databaseHelper.getWritableDatabase();
      return this;
   public void close() {
   public Cursor fetchAllQuestions() {
      return database.query(TABLE_QUESTIONS, new String[] { KEY_ROWID, KEY_QUESTION,
            null, null, null, null, null);
   public Cursor fetchQuestion(long rowId) throws SQLException {
      Cursor cursor = database.query(true, TABLE_QUESTIONS, new String[] { KEY_ROWID,
            KEY_EXPLANATION }, KEY_ROWID + "=" + rowId, null, null, null, null, null);
      if (cursor != null)
      return cursor;

The thing breaks when I try to open the database:
Code: Select all
QuizDbHelper databaseHelper = new QuizDbHelper(this);
databaseHelper.open(); // <-- X(

Now, I'm new at this ("this" being Java) so I'm open to the possibility that I'm doing something remarkably stupid here, but if anyone could point out where I've screwed up I'd be very grateful.
Once Poster
Once Poster
Posts: 1
Joined: Wed Oct 06, 2010 4:22 pm


Re: SQLite and Null Pointer Exception, help please!

Postby blundell » Thu Oct 07, 2010 1:07 pm

Quiz.java: line 88

So use your Quiz.java code, and if that's it the 2 lines above, show us QuizDbHelper.java
User avatar
Master Developer
Master Developer
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK


Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: No registered users and 4 guests