title: Android - NullPointerException on getReadableDatabase()
body: <p>I am writing an app that checks if the first value in the database matches a string. When I try to connect to the database, I get a <code>NullPointerException</code>. This happens on both <code>getReadableDatabase()</code> and <code>getWritableDatabase()</code></p>
  
  <p>Here's my code:</p>
  
  <pre><code>public class DataManager extends SQLiteOpenHelper
  {
      private final static String DB_TABLE = "Nums";
      private final static String COL_KEY      = "KEY";
      private final static String COL_VALUE= "VALUE";
      private static Context context;
      private ContentValues initialValues;
      private SQLiteDatabase db;
      private static DataManager dm;
  
      public static DataManager getInstance(Context _context)
      {
          if (dm==null)
          {dm=new DataManager(_context);}
          return dm;
      }
  
      private DataManager()
      {
          super(context, DB_TABLE, null, 1);
      }
      private DataManager(Context _context)
      {
          super(_context, DB_TABLE, null, 1);
          context=_context;
          initialValues = new ContentValues();
          if (db==null)
          {db=getWritableDatabase();}
      }
  
      @Override
      public void onCreate(SQLiteDatabase db)
      {
          StringBuilder Query = new StringBuilder();
          Query.append("CREATE TABLE IF NOT EXISTS ");
          Query.append(DB_TABLE);
          Query.append('(');
          Query.append(COL_KEY);
              Query.append(" TEXT PRIMARY KEY,");
          Query.append(COL_VALUE);
              Query.append(" TEXT);");
          Log.i(Constants.TAG,"CREATE STRING: "+Query.toString());
          db.execSQL(Query.toString());
  
          if(tableEmpty())
          {setDefault();}
      }
  
      /**
       * Populate the database with numbers 1-&gt;MAXNUM giving each a value of 0.
       */
      private void setDefault()
      {
          for (int i=1;i&lt;=Constants.MAXNUM;i++)
          {
              setValue(String.valueOf(i),"0");
          }
      }
  
      /**
       * Method to get the values, ordered by frequency
       * @return Comma seperated 
       */
      public String[] getAllValues()
      {
          Cursor c=null;
          int counter=0;
          String[] val = new String[Constants.MAXNUM];
  
          try
          {
              c = getReadableDatabase().query(DB_TABLE, null,null, null, null, null, COL_VALUE);
              c.moveToFirst();
              //Ensure there is something in the database
              if(c.getCount()&gt;0)
                  {   // Make sure the cursor never goes over the edge
                      while(!c.isAfterLast())
                      {   // Append each value in order, seperated by a comma
                          val[counter++]=c.getString(1);
                          c.moveToNext();
                      }
                  }
          }
          catch(SQLiteException e){
              Log.e(Constants.TAG,"getValue::SQLiteException::"+e.getMessage());
              e.printStackTrace();
          }
          finally {
              c.close();  // Tidy up
          }
          return val;
      }
  
      public String getValueByKey(String _key)
      {
          String val = "";
          try
          {
              Log.i(Constants.TAG,"key is: "+_key);
              Cursor c=getReadableDatabase().query(DB_TABLE,new String[]{COL_VALUE},COL_KEY + " LIKE ?",new String[]{_key},null,null,null);
              c.moveToFirst();
              if(c.getCount()&gt;0)
                  {   val = c.getString(0);   }
              c.close();
          }
          catch(SQLiteException e)
          {
              Log.e(Constants.TAG,"SQLiteException::"+e.getMessage());
              e.printStackTrace();
          }
  
          return val;
      }
  
      /**
       * Method checks to see if there are any records in the database
       * @return Boolean true if empty, false if not empty
       */
      private boolean tableEmpty()
      {
          boolean result = false;
  
          if(!(getValueByKey("1") == "0") )
          {
              result=true;
          }
  
          return  result;
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
      {}  
  }
  </code></pre>
  
  <p>My database has 2 columns. The left column has the key, and the right has the value. I am trying to get the value of the first record and return it. The error message is a generic NullPointerError so I don't know much about the error, other than the fact that its to do with <code>getReadableDatabase()</code>. Can anyone see what I'm doing wrong?</p>
  
  <p>Thanks.</p>
  
  <p>EDIT: I've added the full code. Here's the stacktrace:</p>
  
  <pre><code>09-23 15:16:27.450: ERROR/AndroidRuntime(11825): FATAL EXCEPTION: main
  09-23 15:16:27.450: ERROR/AndroidRuntime(11825): java.lang.NullPointerException
  09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
  09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at com.eoinzy.myApp.MyClass.openDatabase(MyClass.java:137)
  09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at com.eoinzy.myApp.MyClass.getFrequency(MyClass.java:204)
  09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at com.eoinzy.myApp.MyClass.tableEmpty(MyClass.java:252)
  09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at com.eoinzy.myApp.MyClass.getAllValues(MyClass.java:169)
  09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at com.eoinzy.myApp.MyClass.setupNumbers(MyClass.java:48)
  09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at com.eoinzy.myApp.MyClass.&lt;init&gt;(MyClass.java:38)
  09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at com.eoinzy.myApp.ButtonControl.onClick(ButtonControl.java:56)
  09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at android.view.View.performClick(View.java:2501)
  09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at android.view.View$PerformClick.run(View.java:9107)
  09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at android.os.Handler.handleCallback(Handler.java:587)
  09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at android.os.Handler.dispatchMessage(Handler.java:92)
  09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at android.os.Looper.loop(Looper.java:130)
  09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at android.app.ActivityThread.main(ActivityThread.java:3835)
  09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at java.lang.reflect.Method.invokeNative(Native Method)
  09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at java.lang.reflect.Method.invoke(Method.java:507)
  09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
  09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
  09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>