title: nullPointerException for SQLiteOpenHelper.getWritableDatabase(), Android
body: <p>I am trying to get SQLite database to work on Android.  Wrote a small test in the MainActivity that consists of entering values in 3 collumns of one row of the database. after that the data is read back out and displayed to the screen to verify that it works.</p>
  
  <p>however I am getting a null pointer exception shown in stacktrace from logcat for SQLiteOpenHelper.getWritableDatabase()</p>
  
  <p>does not make much sense to me because the database is created.  Any ideas?</p>
  
  <p>shown below is both parts of the code, the MainActivity class that extends Activity and the Database class that has all the methods for create row, read from, update, and delete.</p>
  
  <p>inside the Database class is a nested class called SQLiteHelper that extends SQLiteOpenHelper.</p>
  
  <p>MainActivity class that extends Activity:</p>
  
  <pre><code>  public class MainActivity extends Activity {
  
  Button buttonOne;
  EditText editTextOne;
  EditText editTextTwo;
  Context context;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      buttonOne = (Button) findViewById(R.id.button1);
      editTextOne = (EditText) findViewById(R.id.edittext1);
      editTextTwo = (EditText) findViewById(R.id.edittext2);
  
               //insert new row
               Database db = new Database(context);
               db.openToWrite();
               db.insertNewRow(1, "testlabel", 210);
  
               Toast.makeText(MainActivity.this,"new row inserted", Toast.LENGTH_SHORT).show();
  
               // display all rows
               int price = 0;
              String name = "";
              int status = 0;
  
            String result = "";
              db.openToRead();
              result = db.getAllFromDB();
              db.close();
              editTextOne.setText(result);
  
  } // end onCreate
     }
  </code></pre>
  
  <p>nested class SQLiteHelper extends SQLiteOpenHelper that is nested inside of Database Class</p>
  
  <pre><code>   public class SQLiteHelper extends SQLiteOpenHelper {
  
            public SQLiteHelper(Context context, String name,
              CursorFactory factory, int version) {
             super(context, name, factory, version);
            }
  
            @Override
            public void onCreate(SQLiteDatabase db) {
             // TODO Auto-generated method stub
             db.execSQL(SCRIPT_CREATE_DATABASE);
            }
  
           }
      }
  </code></pre>
  
  <p>Database class:</p>
  
  <pre><code>   public class Database {
  
      public static final String MYDATABASE_NAME = "my_database";
      public static final String MYDATABASE_TABLE = "my_table";
      public static final int MYDATABASE_VERSION = 1;
      public static final String KEY_CHECKBOX_STATUS = "check_box_status";
      public static final String KEY_CHECKBOX_LABEL = "check_box_label";
      public static final String KEY_PRICE = "price"; 
  
    //create table MY_DATABASE (ID integer primary key, Content text not null);
      private static final String SCRIPT_CREATE_DATABASE =
       "CREATE TABLE " + MYDATABASE_TABLE + " (" + "ID INTEGER PRIMARY KEY AUTOINCREMENT, " +  
      "KEY_CHECKBOX_STATUS INTEGER, " + "KEY_CHECKBOX_LABEL TEXT, " + " KEY_PRICE INTEGER" + ");";
  
      SQLiteDatabase sqLiteDatabase;
      SQLiteHelper sqLiteHelper;
      Context context;
  
      public Database(Context c){
            context = c;
           }
  
      public void openToRead() throws android.database.SQLException {
            sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
            sqLiteDatabase = sqLiteHelper.getReadableDatabase();
           }
  
      public void openToWrite() throws android.database.SQLException {
            sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
            sqLiteDatabase = sqLiteHelper.getWritableDatabase();
           }
  
           public void close(){
           sqLiteHelper.close();
           }
  
      // after this is all the methods for getting and updating and inserts to database
  </code></pre>
  
  <p>EDIT:  full stacktrace from Logcat added below:</p>
  
  <pre><code>FATAL EXCEPTION:
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dbtester/com.example.dbtester.MainActivity}: java.lang.NullPointerException
  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
  android.app.ActivityThread.access$600(ActivityThread.java:123)
  android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
  android.os.Handler.dispatchMessage(Handler.java:99)
  android.os.Looper.loop(Looper.java:137)
  android.app.ActivityThread.main(ActivityThread.java:4424)
  java.lang.reflect.Method.invokeNative(Native Method)
  java.lang.reflect.Method.invoke(Method.java:511)
  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  dalvik.system.NativeStart.main(Native Method)
  caused by: java.lang.NullPointerException
  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
  at com.example.dbtester.Database.openToWrite(Database.java:40)
  at com.example.dbtester.MainActivity.onCreate(MainActivity.java:32)
  android.app.Activity.performCreate(Activity.java:4465)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
  </code></pre>