title: Inserting into SQLite database - Android
body: <p>I am trying to implement a SQLite database for a highscores table.  I am just testing it to see if my database creation and insertion is working with the below code.  But I am getting a NullPointerException on the below lines.</p>
  
  <p>Thank you in advance!</p>
  
  <p>Results.java</p>
  
  <pre><code>public class Results extends Activity {
      DatabaseHelper dh;
  
      public void onCreate(Bundle savedInstanceState) {
          dh = DatabaseHelper.getInstance(this);
  
      public void showResults() {
          dh.openDB();  
          dh.insert(1231423, 436346);  //Line 104
      }
  }
  </code></pre>
  
  <p>DatabaseHelper.java</p>
  
  <pre><code>private static final String SCORE = "score"; 
  private static final String PERCENTAGE = "percentage";
  public static DatabaseHelper mSingleton = null;
  
  public synchronized static DatabaseHelper getInstance(Context context) {
      if(mSingleton == null) {
          mSingleton = new DatabaseHelper(context.getApplicationContext());
      }
      return mSingleton;
  }
  
  public SQLiteDatabase openDB() {
      return this.getWritableDatabase();
  }
  
  public long insert(long score, int percentage) {
      ContentValues values = new ContentValues();
      values.put(SCORE, score);  
      values.put(PERCENTAGE, percentage); 
  
      return db.insert(TABLE, null, values);  //Line 91
  }
  </code></pre>
  
  <p>LogCat output</p>
  
  <pre><code>01-02 17:56:47.609: E/AndroidRuntime(1322): FATAL EXCEPTION: main
  01-02 17:56:47.609: E/AndroidRuntime(1322): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Results}: java.lang.NullPointerException
  01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
  01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
  01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
  01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
  01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.os.Handler.dispatchMessage(Handler.java:99)
  01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.os.Looper.loop(Looper.java:137)
  01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.ActivityThread.main(ActivityThread.java:4745)
  01-02 17:56:47.609: E/AndroidRuntime(1322):     at java.lang.reflect.Method.invokeNative(Native Method)
  01-02 17:56:47.609: E/AndroidRuntime(1322):     at java.lang.reflect.Method.invoke(Method.java:511)
  01-02 17:56:47.609: E/AndroidRuntime(1322):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
  01-02 17:56:47.609: E/AndroidRuntime(1322):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  01-02 17:56:47.609: E/AndroidRuntime(1322):     at dalvik.system.NativeStart.main(Native Method)
  01-02 17:56:47.609: E/AndroidRuntime(1322): Caused by: java.lang.NullPointerException
  01-02 17:56:47.609: E/AndroidRuntime(1322):     at com.example.test.DatabaseHelper.insert(DatabaseHelper.java:91)
  01-02 17:56:47.609: E/AndroidRuntime(1322):     at com.example.test.Results.showResults(Results.java:104)
  01-02 17:56:47.609: E/AndroidRuntime(1322):     at com.example.test.Results.onCreate(Results.java:50)
  01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.Activity.performCreate(Activity.java:5008)
  01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
  01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
  01-02 17:56:47.609: E/AndroidRuntime(1322):     ... 11 more
  </code></pre>
  
  <p><strong>EDIT:</strong> The NullPointerException is at least jumping around but errors still are there.  I have edited my code and posted the new LogCat output.</p>