title: Running database query in FragmentStatePagerAdapter NPE
body: <p>I am trying to run a query against my database to get some data (which changes page to page) from my database.  I am getting a NullPointerException at <code>Double balance = db.getSum();</code>  Below is the full code of my fragmentstatepageradapter</p>
  
  <pre><code>import android.content.Context;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentStatePagerAdapter;
  
  
  public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {
  
      int listSize;
      String[] tabNames;
      String tabName;
      protected Context mContext;
  
      /** Constructor of the class */
      public MyFragmentPagerAdapter(FragmentManager fm, Context c) {
          super(fm);
          mContext = c;
      }
  
      /** This method will be invoked when a page is requested to create */
      @Override
      public Fragment getItem(int arg0) {
  
          MainFragment mainFragment = new MainFragment();
          final DatabaseHelper db = new DatabaseHelper(mContext);
          Double balance = db.getSum(); //NPE here*******************
          tabName = "[" + getPageTitle(arg0) + "]";
          Bundle data = new Bundle();
          data.putInt("current_page", arg0 + 1);
          data.putString("tabName", tabName);
          mainFragment.setArguments(data);
          return mainFragment;
      }
  
      /** Returns the number of pages */
      @Override
      public int getCount() {
          return listSize;
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          return tabNames[position];
      }
  
      public void setCount(int count){
          listSize = count;
      }
  
      public void setNames(String[] names) {
          tabNames = names;
      }
  
      @Override
      public int getItemPosition(Object object) {
          return POSITION_NONE;
      }
  }
  </code></pre>
  
  <p>Here is my database getSum()</p>
  
  <pre><code>// GET SUM
      public Double getSum(){
          SQLiteDatabase db = this.getReadableDatabase();
          Cursor c = db.rawQuery("SELECT SUM(credit), SUM(debit) FROM " + TABLE_DEFAULT_ACCOUNT, null);
          if(c.moveToFirst()) {
              sum = (c.getDouble(0) - c.getDouble(1));
          }
          return sum;
      }
  </code></pre>
  
  <p>And here is the error I am getting</p>
  
  <pre><code>07-09 15:26:52.861  14874-14874/com.rcd.simpleregister D/AndroidRuntime﹕ Shutting down VM
  07-09 15:26:52.865  14874-14874/com.rcd.simpleregister E/AndroidRuntime﹕ FATAL EXCEPTION: main
      Process: com.rcd.simpleregister, PID: 14874
      java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
              at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
              at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
              at com.rcd.simpleregister.DatabaseHelper.getSum(DatabaseHelper.java:90)
              at com.rcd.simpleregister.MyFragmentPagerAdapter.getItem(MyFragmentPagerAdapter.java:31)
              at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:105)
              at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
              at android.support.v4.view.ViewPager.populate(ViewPager.java:982)
              at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
              at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
              at android.view.View.measure(View.java:16537)
              at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
              at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
              at android.view.View.measure(View.java:16537)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5135)
              at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
              at android.view.View.measure(View.java:16537)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5135)
              at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
              at android.view.View.measure(View.java:16537)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5135)
              at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
              at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
              at android.view.View.measure(View.java:16537)
              at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1947)
              at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1137)
              at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1326)
              at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1024)
              at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5796)
              at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
              at android.view.Choreographer.doCallbacks(Choreographer.java:574)
              at android.view.Choreographer.doFrame(Choreographer.java:544)
              at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
              at android.os.Handler.handleCallback(Handler.java:733)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:136)
              at android.app.ActivityThread.main(ActivityThread.java:5102)
              at java.lang.reflect.Method.invoke(Native Method)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  </code></pre>
  
  <p>Anyone see why I might be getting this?</p>
  
  <p>Thanks!</p>