title: RecyclerView on viewPager crashes
body: <p>At first my recyclerview are running smoothly from scratch without ViewPager. But when i try to add my recyclerview on my ViewPager Adapter it crashes and it says the error is in the Database Helper in getAllData(). TY in advance. hope someone can help me figure this out.</p>
  
  <p>This is my code.</p>
  
  <p>FragmentMeal.java </p>
  
  <pre><code>package inncharge.poy.madrigal.innchargev1.fragments;
  
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v7.widget.DefaultItemAnimator;
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.RecyclerView;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  import inncharge.poy.madrigal.innchargev1.R;
  import inncharge.poy.madrigal.innchargev1.activities.DatabaseHelper;
  import inncharge.poy.madrigal.innchargev1.adapters.DataBeanAdapter;
  import inncharge.poy.madrigal.innchargev1.pojo.DataBean;
  
  
  /**
   * A simple {@link Fragment} subclass.
   * Use the {@link FragmentMeal#newInstance} factory method to
   * create an instance of this fragment.
   */
  public class FragmentMeal extends Fragment {
  
  
  
      // TODO: Rename parameter arguments, choose names that match
      // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
      private static final String ARG_PARAM1 = "param1";
      private static final String ARG_PARAM2 = "param2";
  
      // TODO: Rename and change types of parameters
      private String mParam1;
      private String mParam2;
      DatabaseHelper helper = new DatabaseHelper(getActivity());
      private RecyclerView mRecyclerView;
  
  
      /**
       * Use this factory method to create a new instance of
       * this fragment using the provided parameters.
       *
       * @param param1 Parameter 1.
       * @param param2 Parameter 2.
       * @return A new instance of fragment FragmentMeal.
       */
      // TODO: Rename and change types and number of parameters
      public static android.support.v4.app.Fragment newInstance(String param1, String param2) {
          FragmentMeal fragment = new FragmentMeal();
          Bundle args = new Bundle();
          args.putString(ARG_PARAM1, param1);
          args.putString(ARG_PARAM2, param2);
          fragment.setArguments(args);
          return fragment;
      }
  
      public FragmentMeal() {
          // Required empty public constructor
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          if (getArguments() != null) {
              mParam1 = getArguments().getString(ARG_PARAM1);
              mParam2 = getArguments().getString(ARG_PARAM2);
          }
  
      }
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          View view = inflater.inflate(R.layout.fragment_meal, container, false);
          mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
          mRecyclerView.setHasFixedSize(true);
          mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
          mRecyclerView.setItemAnimator(new DefaultItemAnimator());
          mRecyclerView.setAdapter(new DataBeanAdapter(helper.getAllData(), R.layout.item));
          return view;
      }
  
  
  
  }
  </code></pre>
  
  <p>DatabaseHelper.java</p>
  
  <pre><code>    package inncharge.poy.madrigal.innchargev1.activities;
  
      import android.content.ContentValues;
      import android.content.Context;
      import android.database.Cursor;
      import android.database.sqlite.SQLiteDatabase;
      import android.database.sqlite.SQLiteOpenHelper;
  
      import java.util.ArrayList;
      import java.util.List;
  
      import inncharge.poy.madrigal.innchargev1.pojo.DataBean;
  
      /**
       * Created by Madrigal on 8/9/2015.
       */
      public class DatabaseHelper extends SQLiteOpenHelper {
  
          private  static final int DATABASE_VERSION = 1;
          private  static final String DATABASE_NAME = "contacts.db";
          private  static final String TABLE_NAME = "contacts";
          private  static final String UID = "id";
          private  static final String NAME = "name";
          private  static final String CARD = "card";
          private  static final String CODE = "code";
          SQLiteDatabase db;
          private static final String TABLE_CREATE = "CREATE TABLE contacts (id INTEGER PRIMARY KEY NOT NULL," +
                  "name TEXT NOT NULL , card TEXT NOT NULL, code TEXT NOT NULL)";
  
          public DatabaseHelper(Context context) {
              super(context, DATABASE_NAME, null, DATABASE_VERSION);
  
          }
  
          @Override
          public void onCreate(SQLiteDatabase db) {
              db.execSQL(TABLE_CREATE);
              this.db = db;
          }
  
          public void insertBean(DataBean bean) {
  
              db = this.getWritableDatabase();
              ContentValues values = new ContentValues();
  
              String query = "SELECT * FROM contacts";
              Cursor cursor = db.rawQuery(query, null);
              int count = cursor.getCount();
  
              values.put(UID, count);
              values.put(NAME, bean.getName());
              values.put(CARD, bean.getCard());
              values.put(CODE, bean.getCode());
  
              db.insert(TABLE_NAME, null, values);
              db.close();
  
          }
  
          public List&lt;DataBean&gt; getAllData() {
              db = this.getWritableDatabase();
              String query = "SELECT * FROM contacts";
              Cursor cursor = db.rawQuery(query, null);
  
              List&lt;DataBean&gt; list = new ArrayList&lt;&gt;();
  
              while (cursor.moveToNext()) {
                  int index = cursor.getColumnIndex(UID);
                  int index2 = cursor.getColumnIndex(NAME);
                  int index3 = cursor.getColumnIndex(CARD);
                  int index4 = cursor.getColumnIndex(CODE);
                  int id = cursor.getInt(index);
                  String name = cursor.getString(index2);
                  String card = cursor.getString(index3);
                  String code = cursor.getString(index4);
                  DataBean bean = new DataBean();
                  bean.setId(id);
                  bean.setName(name);
                  bean.setCard(card);
                  bean.setCode(code);
                  list.add(bean);
              }
              return list;
          }
  
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          String query = "DROP TABLE IF EXIST"+TABLE_NAME;
          db.execSQL(query);
          this.onCreate(db);
      }
  }
  </code></pre>
  
  <p>ViewPagerAdapater</p>
  
  <pre><code>private class ViewPagerAdapter extends FragmentStatePagerAdapter {
  
          int icons[] = {R.drawable.ic_action_meal,
                  R.drawable.ic_action_drinks,
                  R.drawable.ic_action_snack};
  
          public ViewPagerAdapter(FragmentManager fm) {
              super(fm);
          }
  
          public Fragment getItem(int num) {
  
              Fragment fragment = null;
  
              switch (num) {
                  case MEAL:
                      fragment = FragmentMeal.newInstance("", "");
                      break;
                  case DRINKS:
                      fragment = FragmentDrinks.newInstance("", "");
                      break;
                  case SNACK:
                      fragment = FragmentSnack.newInstance("", "");
                      break;
              }
                  return fragment;
          }
  
          @Override
          public int getCount() {
  
              return 3;
          }
  
          @Override
          public CharSequence getPageTitle(int position) {
              return getResources().getStringArray(R.array.tabs)[position];
          }
  
          private Drawable getIcon(int position) {
  
              return ResourcesCompat.getDrawable(getResources(), icons[position], null);
  
          }
  
      }
  </code></pre>
  
  <p>Adapter</p>
  
  <pre><code>package inncharge.poy.madrigal.innchargev1.adapters;
  
  import android.support.v7.widget.RecyclerView;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.TextView;
  
  import java.util.List;
  
  import inncharge.poy.madrigal.innchargev1.R;
  import inncharge.poy.madrigal.innchargev1.pojo.DataBean;
  
  /**
   * Created by Madrigal on 8/17/2015.
   */
  public class DataBeanAdapter extends RecyclerView.Adapter&lt;DataBeanAdapter.ViewHolder&gt;{
      private List&lt;DataBean&gt; items;
      private int itemLayout;
  
      public DataBeanAdapter(List&lt;DataBean&gt; items, int itemLayout){
          this.items = items;
          this.itemLayout = itemLayout;
      }
  
      @Override
      public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View v = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false);
          return new ViewHolder(v);
      }
  
      @Override
      public void onBindViewHolder(ViewHolder holder, int position) {
          DataBean item = items.get(position);
          holder.name.setText(item.getName());
          holder.card.setText(item.getCard());
          //All the thing you gonna show in the item
      }
  
      @Override
      public int getItemCount() {
          return items.size();
      }
  
      public static class ViewHolder extends RecyclerView.ViewHolder {
          public TextView name;
          public TextView card;
  
          public ViewHolder(View itemView) {
              super(itemView);
              name = (TextView) itemView.findViewById(R.id.name);
              card = (TextView) itemView.findViewById(R.id.card);
          }
      }
  }
  </code></pre>
  
  <p><strong>Edit added logcat from ops comment</strong></p>
  
  <blockquote>
    <p>08-17 17:17:26.299 1333-1333/? E/AndroidRuntime﹕    FATAL EXCEPTION:
    main java.lang.NullPointerException    at 
    android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java‌​:224)
    at
    android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.ja‌​va:164)
    at
    inncharge.poy.madrigal.innchargev1.activities.DatabaseHelper.getAllData(Database‌​Helper.java:61)
    at
    inncharge.poy.madrigal.innchargev1.fragments.FragmentMeal.onCreateView(FragmentM‌​eal.java:82)</p>
    
    <p>java:82 error is this.  mRecyclerView.setAdapter(new
    DataBeanAdapter(helper.getAllData(), R.layout.item));</p>
    
    <p>lang java:61 erro is this. public List getAllData() { db =
    this.getWritableDatabase();</p>
  </blockquote>
  
  <p>TAKE NOTE:<br>
  when I try my recycler view without viewpager it works perfectly, but when I try to add it to my view pager. This error occurs. </p>