title: Android app writing to SQLite database
body: <p>I am trying to write to the local SQLlite database on my android app.
  I have created a method in the handler that opens the database, but it is crashing the app.
  I know from using logs that it is the line:</p>
  
  <pre><code>db = helper.getWritableDatabase();
  </code></pre>
  
  <p>That is causing the crash, but I have no idea why.</p>
  
  <p>Below is the handler class:</p>
  
  <pre><code>public static final String TAG = "TableSubandObAssessment";
  
      public static final String KEY_TABLE = "assessments";
  
      private static final String KEY_APPOINTMENT_ID = "appointmentID";
      private static final String KEY_TYPE = "type";
      private static final String KEY_ASSESSMENT = "assessment";
      private static final String KEY_VARIABLE1 = "variable1";
      private static final String KEY_VARIABLE2 = "vriable2";
      private static final String KEY_VARIABLE3 = "vriable3";
      private static final String KEY_VARIABLE4 = "vriable4";
  
  
      private static DatabaseHelper helper;
      private static Context context;
      public static SQLiteDatabase db;
  
  
      static public void createTable(SQLiteDatabase db) {
          String createTbl = 
                  "CREATE TABLE " + KEY_TABLE
                  + " (" + KEY_APPOINTMENT_ID + " INTEGER PRIMARY KEY NOT NULL, " 
                  + KEY_TYPE + " TEXT NOT NULL, " 
                  + KEY_ASSESSMENT + " TEXT NOT NULL, "
                  + KEY_VARIABLE1 + " TEXT, "
                  + KEY_VARIABLE2 + " TEXT NOT NULL, "
                  + KEY_VARIABLE3 + " TEXT, "
                  + KEY_VARIABLE4 + " TEXT " + ");";
      db.execSQL(createTbl);
      }
  
  
  
  
      public static void dropTable(SQLiteDatabase db) {
          db.execSQL("DROP TABLE IF EXISTS " + KEY_TABLE);
          createTable(db);
      }   
  
  
      public TableSubandObAssessment(Context context) {
          this.context = context;
      }
  
      public static SQLiteDatabase open() throws SQLException {
          helper = new DatabaseHelper(context);
          db = helper.getWritableDatabase();
          return db;
      }
  
      public static void close() {
          helper.close();
      }
  
  
        public static long insertTableSubandObAssessment(int appointmentID, String type, String assessment, String variable1, String variable2, String variable3, String variable4) {
              ContentValues values = new ContentValues();
              values.put(KEY_APPOINTMENT_ID, appointmentID);
              values.put(KEY_TYPE, type);
              values.put(KEY_ASSESSMENT, assessment);
              values.put(KEY_VARIABLE1, variable1);
              values.put(KEY_VARIABLE2, variable2);
              values.put(KEY_VARIABLE3, variable3);
              values.put(KEY_VARIABLE4, variable4);
              return db.insert(KEY_TABLE, null, values);
          }
  </code></pre>
  
  <p>and here is the logcat:</p>
  
  <pre><code>08-07 14:34:55.806: E/AndroidRuntime(1995): FATAL EXCEPTION: main
  08-07 14:34:55.806: E/AndroidRuntime(1995): java.lang.NullPointerException
  08-07 14:34:55.806: E/AndroidRuntime(1995):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
  08-07 14:34:55.806: E/AndroidRuntime(1995):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
  08-07 14:34:55.806: E/AndroidRuntime(1995):     at com.ucl.pga.db.TableSubandObAssessment.open(TableSubandObAssessment.java:58)
  08-07 14:34:55.806: E/AndroidRuntime(1995):     at com.ucl.pga.db.Objective.Summary.saveToDb(Summary.java:965)
  08-07 14:34:55.806: E/AndroidRuntime(1995):     at com.ucl.pga.db.Objective.Summary$2$1.onClick(Summary.java:480)
  08-07 14:34:55.806: E/AndroidRuntime(1995):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
  08-07 14:34:55.806: E/AndroidRuntime(1995):     at android.os.Handler.dispatchMessage(Handler.java:99)
  08-07 14:34:55.806: E/AndroidRuntime(1995):     at android.os.Looper.loop(Looper.java:137)
  08-07 14:34:55.806: E/AndroidRuntime(1995):     at android.app.ActivityThread.main(ActivityThread.java:4745)
  08-07 14:34:55.806: E/AndroidRuntime(1995):     at java.lang.reflect.Method.invokeNative(Native Method)
  08-07 14:34:55.806: E/AndroidRuntime(1995):     at java.lang.reflect.Method.invoke(Method.java:511)
  08-07 14:34:55.806: E/AndroidRuntime(1995):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
  08-07 14:34:55.806: E/AndroidRuntime(1995):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  08-07 14:34:55.806: E/AndroidRuntime(1995):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>