title: App stops whenever I press back button
body: <p>Problem: This only happening on some pages. when I press back app stops. I get this error. I am not able to figure it out the exact problem. I am new to android. and this is happening at many pages.Any help would be highly appreciated.</p>
  
  <p>logcat error: </p>
  
  <pre><code>    InputEventSender:Exception dispatching finished signal.E/MessageQueue-JNI:Exception in MessageQueue callback:handleReceiveCallbackE/MessageQueue-JNI:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.database.sqlite.SQLiteDatabase.close()' on a null object reference at twik.in.DBHelper.close(DBHelper.java:102)at twik.in.ActivityMenuDetail.onBackPressed(ActivityMenuDetail.java:285)at android.app.Activity.onKeyUp(Activity.java:2477)at android.view.KeyEvent.dispatch(KeyEvent.java:2664)at android.app.Activity.dispatchKeyEvent(Activity.java:2730)  
  </code></pre>
  
  <p>In error these two classes are mentioned so I am posting its code here</p>
  
  <pre><code>public class ActivityMenuDetail extends Activity {
  
  ImageView imgPreview;
  TextView txtText, txtSubText;
  WebView txtDescription;
  Button btnAdd;
  ScrollView sclDetail;
  ProgressBar prgLoading;
  TextView txtAlert;
  
  // declare dbhelper object
  static DBHelper dbhelper;
  
  // declare ImageLoader object
  ImageLoader imageLoader;
  
  // declare variables to store menu data
  String Menu_image, Menu_name, Menu_serve, Menu_description;
  double Menu_price;
  int Menu_quantity;
  long Menu_ID;
  String MenuDetailAPI;
  int IOConnect = 0;
  
  // create price format
  DecimalFormat formatData = new DecimalFormat("#.##");
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.menu_detail);
  
      ActionBar bar = getActionBar();
      bar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.header)));
      bar.setTitle("Service Centre Detail");
      bar.setDisplayHomeAsUpEnabled(true);
      bar.setHomeButtonEnabled(true);
  
      imgPreview = (ImageView) findViewById(R.id.imgPreview);
      txtText = (TextView) findViewById(R.id.txtText);
      txtSubText = (TextView) findViewById(R.id.txtSubText);
      txtDescription = (WebView) findViewById(R.id.txtDescription);
      btnAdd = (Button) findViewById(R.id.btnAdd);
      //btnShare = (Button) findViewById(R.id.btnShare);
      sclDetail = (ScrollView) findViewById(R.id.sclDetail);
      prgLoading = (ProgressBar) findViewById(R.id.prgLoading);
      txtAlert = (TextView) findViewById(R.id.txtAlert);
  
      // get screen device width and height
      DisplayMetrics dm = new DisplayMetrics();
      getWindowManager().getDefaultDisplay().getMetrics(dm);
      int wPix = dm.widthPixels;
      int hPix = wPix / 2 + 50;
  
      // change menu image width and height
      LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(wPix, hPix);
      imgPreview.setLayoutParams(lp);
  
      imageLoader = new ImageLoader(ActivityMenuDetail.this);
      dbhelper = new DBHelper(this);
  
      // get menu id that sent from previous page
      Intent iGet = getIntent();
      Menu_ID = iGet.getLongExtra("menu_id", 0);
  
      // Menu detail API url
      MenuDetailAPI = Constant.MenuDetailAPI+"?accesskey="+Constant.AccessKey+"&amp;menu_id="+Menu_ID;
  
      // call asynctask class to request data from server
      new getDataTask().execute();      
  
      // event listener to handle add button when clicked
      btnAdd.setOnClickListener(new OnClickListener() {
  
          public void onClick(View arg0) {
              // TODO Auto-generated method stub
              // show input dialog
              addtoCart();
          }
      });
  
  
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_detail, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      switch (item.getItemId()) {
      case R.id.cart:
          // refresh action
          Intent iMyOrder = new Intent(ActivityMenuDetail.this, ActivityCart.class);
          startActivity(iMyOrder);
          overridePendingTransition (R.anim.open_next, R.anim.close_next);
          return true;
  
      case android.R.id.home:
          // app icon in action bar clicked; go home
          this.finish();
          overridePendingTransition(R.anim.open_main, R.anim.close_next);
          return true;
  
      default:
          return super.onOptionsItemSelected(item);
      }
  
  }
  
  // method to show number of order form
  
  void addtoCart(){
       // open database first
      try{
          dbhelper.openDataBase();
      }catch(SQLException sqle){
          throw sqle;
      }
  
      if(dbhelper.isDataExist(Menu_ID)){
          dbhelper.updateData(Menu_ID, 1, (Menu_price));
      }else{
          dbhelper.addData(Menu_ID, Menu_name, 1, (Menu_price));
      }
  
      startActivity(new Intent(ActivityMenuDetail.this,ActivityCart.class));
  
  };
  
  // asynctask class to handle parsing json in background
  public class getDataTask extends AsyncTask&lt;Void, Void, Void&gt;{
  
      // show progressbar first
      getDataTask(){
          if(!prgLoading.isShown()){
              prgLoading.setVisibility(0);
              txtAlert.setVisibility(8);
          }
      }
  
      @Override
      protected Void doInBackground(Void... arg0) {
          // TODO Auto-generated method stub
          // parse json data from server in background
          parseJSONData();
          return null;
      }
  
      @Override
      protected void onPostExecute(Void result) {
          // TODO Auto-generated method stub
          // when finish parsing, hide progressbar
          prgLoading.setVisibility(8);
          // if internet connection and data available show data
          // otherwise, show alert text
          if((Menu_name != null) &amp;&amp; IOConnect == 0){
              sclDetail.setVisibility(0);
  
              imageLoader.DisplayImage(Constant.AdminPageURL+Menu_image, imgPreview);
  
              txtText.setText(Menu_name);
              txtSubText.setText("Price : " +Menu_price+" "+ActivityMenuList.Currency+"\n"+"Status : "+Menu_serve+"\n"+"Empty Slots : "+Menu_quantity);
              txtDescription.loadDataWithBaseURL("", Menu_description, "text/html", "UTF-8", "");
              txtDescription.setBackgroundColor(Color.parseColor("#e7e7e7"));
          }else{
              txtAlert.setVisibility(0);
          }
      }
  }
  
  // method to parse json data from server
  public void parseJSONData(){
  
      try {
          // request data from menu detail API
          HttpClient client = new DefaultHttpClient();
          HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000);
          HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
          HttpUriRequest request = new HttpGet(MenuDetailAPI);
          HttpResponse response = client.execute(request);
          InputStream atomInputStream = response.getEntity().getContent();
  
  
          BufferedReader in = new BufferedReader(new InputStreamReader(atomInputStream));
  
          String line;
          String str = "";
          while ((line = in.readLine()) != null){
              str += line;
          }
  
          // parse json data and store into tax and currency variables
          JSONObject json = new JSONObject(str);
          JSONArray data = json.getJSONArray("data"); // this is the "items: [ ] part
  
          for (int i = 0; i &lt; data.length(); i++) {
              JSONObject object = data.getJSONObject(i); 
  
              JSONObject menu = object.getJSONObject("Menu_detail");
  
              Menu_image = menu.getString("Menu_image");
              Menu_name = menu.getString("Menu_name");
              Menu_price = Double.valueOf(formatData.format(menu.getDouble("Price")));
              Menu_serve = menu.getString("Serve_for");
              Menu_description = menu.getString("Description");
              Menu_quantity = menu.getInt("Quantity");
  
          }
  
  
      } catch (MalformedURLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          IOConnect = 1;
          e.printStackTrace();
      } catch (JSONException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }   
  }
  
  
  // close database before back to previous page
  @Override
  public void onBackPressed() {
      // TODO Auto-generated method stub
      super.onBackPressed();
      dbhelper.close();
      finish();
      overridePendingTransition(R.anim.open_main, R.anim.close_next);
  }
  
  
  @Override
  protected void onDestroy() {
      // TODO Auto-generated method stub
      //imageLoader.clearCache();
      super.onDestroy();
  }
  
  
  @Override
  public void onConfigurationChanged(final Configuration newConfig)
  {
      // Ignore orientation change to keep activity from restarting
      super.onConfigurationChanged(newConfig);
  }
  }
  </code></pre>
  
  <p>and debhelper class here</p>
  
  <pre><code>public class DBHelper extends SQLiteOpenHelper{
  
  String DB_PATH;
  private final static String DB_NAME = "db_order";
  public final static int DB_VERSION = 1;
  public static SQLiteDatabase db; 
  
  private final Context context;
  
  private final String TABLE_NAME = "tbl_order";
  private final String ID = "id";
  private final String MENU_NAME = "Menu_name";
  private final String QUANTITY = "Quantity";
  private final String TOTAL_PRICE = "Total_price";
  
  
  public DBHelper(Context context) {
  
      super(context, DB_NAME, null, DB_VERSION);
      this.context = context;
  
      DB_PATH = Constant.DBPath;
  }   
  
  public void createDataBase() throws IOException{
  
      boolean dbExist = checkDataBase();
      SQLiteDatabase db_Read = null;
  
  
      if(dbExist){
          //do nothing - database already exist
  
      }else{
          db_Read = this.getReadableDatabase();
          db_Read.close();
  
          try {
              copyDataBase();
          } catch (IOException e) {
              throw new Error("Error copying database");
          }
      }
  
  }
  
  
  
  private boolean checkDataBase(){
  
      File dbFile = new File(DB_PATH + DB_NAME);
  
      return dbFile.exists();
  
  }
  
  
  private void copyDataBase() throws IOException{
  
      InputStream myInput = context.getAssets().open(DB_NAME);
  
      String outFileName = DB_PATH + DB_NAME;
  
      OutputStream myOutput = new FileOutputStream(outFileName);
  
      byte[] buffer = new byte[1024];
      int length;
      while ((length = myInput.read(buffer))&gt;0){
          myOutput.write(buffer, 0, length);
      }
  
      myOutput.flush();
      myOutput.close();
      myInput.close();
  
  }
  
  public void openDataBase() throws SQLException{
      String myPath = DB_PATH + DB_NAME;
      db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
  }
  
  @Override
  public void close() {
      db.close();
  }
  
  @Override
  public void onCreate(SQLiteDatabase db) {
  
  }
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  
  }
  
  /** this code is used to get all data from database */
  public ArrayList&lt;ArrayList&lt;Object&gt;&gt; getAllData(){
      ArrayList&lt;ArrayList&lt;Object&gt;&gt; dataArrays = new ArrayList&lt;ArrayList&lt;Object&gt;&gt;();
  
      Cursor cursor = null;
  
          try{
              cursor = db.query(
                      TABLE_NAME,
                      new String[]{ID, MENU_NAME, QUANTITY, TOTAL_PRICE},
                      null,null, null, null, null);
              cursor.moveToFirst();
  
              if (!cursor.isAfterLast()){
                  do{
                      ArrayList&lt;Object&gt; dataList = new ArrayList&lt;Object&gt;();
  
                      dataList.add(cursor.getLong(0));
                      dataList.add(cursor.getString(1));
                      dataList.add(cursor.getString(2));
                      dataList.add(cursor.getString(3));
  
                      dataArrays.add(dataList);
                  }
  
                  while (cursor.moveToNext());
              }
              cursor.close();
          }catch (SQLException e){
              Log.e("DB Error", e.toString());
              e.printStackTrace();
          }
  
      return dataArrays;
  }
  
  /** this code is used to get all data from database */
  public boolean isDataExist(long id){
      boolean exist = false;
  
      Cursor cursor = null;
  
          try{
              cursor = db.query(
                      TABLE_NAME,
                      new String[]{ID},
                      ID +"="+id,
                      null, null, null, null);
              if(cursor.getCount() &gt; 0){
                  exist = true;
              }
  
              cursor.close();
          }catch (SQLException e){
              Log.e("DB Error", e.toString());
              e.printStackTrace();
          }
  
      return exist;
  }
  
  /** this code is used to get all data from database */
  public boolean isPreviousDataExist(){
      boolean exist = false;
  
      Cursor cursor = null;
  
          try{
              cursor = db.query(
                      TABLE_NAME,
                      new String[]{ID},
                      null,null, null, null, null);
              if(cursor.getCount() &gt; 0){
                  exist = true;
              }
  
              cursor.close();
          }catch (SQLException e){
              Log.e("DB Error", e.toString());
              e.printStackTrace();
          }
  
      return exist;
  }
  
  public void addData(long id, String menu_name, int quantity, double total_price){
      // this is a key value pair holder used by android's SQLite functions
      ContentValues values = new ContentValues();
      values.put(ID, id);
      values.put(MENU_NAME, menu_name);
      values.put(QUANTITY, quantity);
      values.put(TOTAL_PRICE, total_price);
  
      // ask the database object to insert the new data 
      try{db.insert(TABLE_NAME, null, values);}
      catch(Exception e)
      {
          Log.e("DB ERROR", e.toString());
          e.printStackTrace();
      }
  }
  
  public void deleteData(long id){
      // ask the database manager to delete the row of given id
      try {db.delete(TABLE_NAME, ID + "=" + id, null);}
      catch (Exception e)
      {
          Log.e("DB ERROR", e.toString());
          e.printStackTrace();
      }
  }
  
  public void deleteAllData(){
      // ask the database manager to delete the row of given id
      try {db.delete(TABLE_NAME, null, null);}
      catch (Exception e)
      {
          Log.e("DB ERROR", e.toString());
          e.printStackTrace();
      }
  }
  
  public void updateData(long id, int quantity, double total_price){
      // this is a key value pair holder used by android's SQLite functions
      ContentValues values = new ContentValues();
      values.put(QUANTITY, quantity);
      values.put(TOTAL_PRICE, total_price);
  
      // ask the database object to update the database row of given rowID
      try {db.update(TABLE_NAME, values, ID + "=" + id, null);}
      catch (Exception e)
      {
          Log.e("DB Error", e.toString());
          e.printStackTrace();
      }
  }}
  </code></pre>