title: SQlite null database
body: <p>I am building a simple bank app (no security what so ever) and have a login activity that verifies the input with an SQLite database. My method returns 1 if is a match and 0 if not. The problem is it always returns 0 because the database is null according to debug. I'm not sure why the variable I use in the login method is null. The data insert is working as I've checked using SQLite browser.</p>
  
  <p>Here is the SQLite database with the login() method at the bottom:</p>
  
  <pre><code>public class CustomerDatabase{
  
  
  
  private SQLiteDatabase database;
  private SQLiteOpenHelper openHelper;
  
  //Database constants
  public static final String DB_Name = "customers.db";
  public static final int DB_Version = 1;
  
  public static final String CUSTOMER_TABLE = "Customers";
  
  public static final String CARD_ACCESS_NO = "accessNo";
  public static final int CARD_ACCESS_NO_COLUMN = 0;
  
  public static final String CUSTOMER_PASSWORD = "password";
  public static final int CUSTOMER_PASSWORD_COLUMN = 2;
  
  public static final String CUSTOMER_NAME = "custName";
  public static final int CUSTOMER_NAME_COLUMN = 1;
  
  public static final String CHECKING_ACCOUNT_FUNDS = "checkAccFunds";
  public static final int CHECKING_ACCOUNT_FUNDS_COLUMN = 3;
  
  public static final String SAVINGS_ACCOUNT_FUNDS = "saveAccFunds";
  public static final int SAVINGS_ACCOUNT_FUNDS_COLUMN = 4;
  
  public static final String CHECKING_ACCOUNT_NO = "checkAccNo";
  public static final int CHECKING_ACCOUNT_NO_COLUMN = 5;
  
  public static final String SAVINGS_ACCOUNT_NO = "saveAccNo";
  public static final int SAVINGS_ACCOUNT_NO_COLUMN = 6;
  
  //DDL for creating the db
  public static final String CREATE_CUSTOMER_TABLE =
          "CREATE TABLE " + CUSTOMER_TABLE + " (" +
                  CARD_ACCESS_NO + " INTEGER PRIMARY KEY, " +
                  CUSTOMER_NAME + " TEXT, " +
                  CHECKING_ACCOUNT_NO + " TEXT, " +
                  CUSTOMER_PASSWORD + " TEXT, " +
                  CHECKING_ACCOUNT_FUNDS + " TEXT, " +
                  SAVINGS_ACCOUNT_NO + " TEXT, " +
                  SAVINGS_ACCOUNT_FUNDS + " TEXT)";
  
  
  public CustomerDatabase(Context context){
      openHelper = new DBHelper(context, DB_Name, DB_Version);
  }
  
  //method to store accountHolder objects into the database
  public AccountHolder saveAccHolder(AccountHolder holder){
      database = openHelper.getWritableDatabase();
  
      ContentValues values = new ContentValues();
      values.put(CARD_ACCESS_NO, holder.getAccessCardNo());
      values.put(CUSTOMER_NAME, holder.getName());
      values.put(CUSTOMER_PASSWORD, holder.getPassword());
      values.put(CHECKING_ACCOUNT_FUNDS, holder.getCheckingAccountFunds());
      values.put(CHECKING_ACCOUNT_NO, holder.getCheckingAccountNo());
      values.put(SAVINGS_ACCOUNT_NO, holder.getSavingsAccountNo());
      values.put(SAVINGS_ACCOUNT_FUNDS, holder.getSavingsAccountFunds());
  
      long id = database.insert(CUSTOMER_TABLE, null, values);
  
      holder.setDbId(id);
  
      database.close();
  
      return holder;
  }
  
  private static class DBHelper extends SQLiteOpenHelper{
  
      public DBHelper(Context context, String name, int version){
          super(context, name, null, version);
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) {
  
          db.execSQL("DROP TABLE IF EXISTS " + CUSTOMER_TABLE);
          db.execSQL(CREATE_CUSTOMER_TABLE);
  
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
  
          db.execSQL("DROP TABLE IF EXISTS " + CUSTOMER_TABLE);
          onCreate(db);
  
      }
  }
  
  /**Method will be used by the bank to check if user is authorized */
  public int login(String accessNo, String password){
      String[] selectArgs = new String[]{accessNo,password};
  
      try{
          int i = 0;
          Cursor cursor = null;
          cursor = database.rawQuery("SELECT * FROM CUSTOMER_TABLE WHERE accessNo =? AND password=?",selectArgs);
          cursor.moveToFirst();
          i = cursor.getCount();
          cursor.close();
          return i;
      }
      catch (Exception e){ e.printStackTrace();}
  
      return 0;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Debug says that the database variable is null at login() at line cursor = database.rawQuery()...</p>
  
  <p>Here is the login activity logic:</p>
  
  <pre><code>btnLogin.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              if(edtAccessCardNo.length() != 0 || edtPassword.length() != 0){
                  String accessNoInput = edtAccessCardNo.getText().toString();
                  String passwordInput = edtPassword.getText().toString();
  
                  boolean checkVerify = isVerifiedUser(accessNoInput, passwordInput);
  
                  if(checkVerify){
                      Intent intent = new Intent(getApplicationContext(), BankActivity.class);
                      startActivity(intent);
                  }else{
                      txtErrorDisplay.setText("Unable to login.");
                  }
              }else{
                  edtAccessCardNo.setHint("This is a required field");
                  edtPassword.setHint("This is a required field");
              }
          }
      });
  
  }
  
  //Method to create dummy accountHolder objects
  public void getAndSaveDummyHolders(){
      CustomerDatabase custDb = new CustomerDatabase(this);
  
      for(int i = 0; i &lt; 5; i++){
          AccountHolder holder = new AccountHolder(
                  holderNames[i], accessNo[i],
                  passwords[i], checkAccNo[i], savAccNo[i],
                  checkAccFunds[i], savAccFunds[i]);
  
          custDb.saveAccHolder(holder);
      }
  
  
  }
  
  //verify the user using method in customer database
  public boolean isVerifiedUser(String userAccessCard, String userPassword){
      CustomerDatabase custDb = new CustomerDatabase(getApplicationContext());
  
      int check = custDb.login(userAccessCard, userPassword);
  
      if(check == 1){
          return true;
      }else{
          return false;
      }
  }
  </code></pre>