title: My app is crashing when I run the AVD and click a button that attempts to save username and password on the database
body: <p>I'm using Android Studio with the SQLite tool to try to build a simple register screen, but when I run the AVD and click the my register button (It's called cadastrar), the app crashes. </p>
  
  <p>I have searched Stack Overflow and other sites for a solution and found similar questions but couldn't really grasp the logic behind the explanation (Im new to programming, so I mostly watch youtube tutorials and do a lot of copy paste while trying to understand the code).</p>
  
  <p>This is DbHelper.java:</p>
  
  <pre><code>package com.example.gerenciadorestoqueoficial;
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.content.ContextWrapper;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  
  public class DbHelper extends SQLiteOpenHelper {
      public static final String DATABASE_NAME = "register.db";
      public static final String TABLE_NAME = "cadastrarusuario";
      public static final String COL_1 = "ID";
      public static final String COL_2 = "nome";
      public static final String COL_3 = "password";
  
      public DbHelper(Context context) {
          super(context, DATABASE_NAME, null , 1);
      }
  
      @Override
      public void onCreate(SQLiteDatabase sqLiteDatabase) {
          sqLiteDatabase.execSQL("CREATE TABLE cadastrarusuario (ID INTEGER PRIMARY KEY AUTOINCREMENT, nome TEXT, password TEXT)");
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
          sqLiteDatabase.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME );
          onCreate(sqLiteDatabase);
      }
  
      public long adicionarUsuario(String usuario, String password){
          SQLiteDatabase db = this.getWritableDatabase();
          ContentValues contentValues = new ContentValues();
          contentValues.put("usuario", usuario);
          contentValues.put("password", password);
          long res = db.insert("cadastrarusuario", null, contentValues);
          db.close();
          return res;
      }
      public boolean checkUser(String nome, String password){
          String[] columns = { COL_1 };
          SQLiteDatabase db = getReadableDatabase();
          String selection = COL_1 + "=?" + " e " + COL_3 + "=?";
          String[] selectionArgs = {nome, password};
          Cursor cursor = db.query(TABLE_NAME,columns,selection,selectionArgs,null,null,null);
          int count = cursor.getCount();
          cursor.close();
          db.close();
  
          if(count&gt;0)
              return true;
          else
              return false;
      }
  }
  </code></pre>
  
  <p>This is Cadastro.Java:</p>
  
  <pre><code>package com.example.gerenciadorestoqueoficial;
  
  import android.content.Intent;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class Cadastro extends AppCompatActivity {
  
  
      EditText cadastroNome;
      EditText cadastroPassword;
      EditText cnfCadastroPassword;
      Button cadastrar;
      TextView verLogin;
      DbHelper db;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.tela_cadastro);
  
  
          cadastroNome = (EditText)findViewById(R.id.etCadastroNome);
          cadastroPassword = (EditText)findViewById(R.id.etCadastroPassword);
          cnfCadastroPassword =(EditText)findViewById(R.id.etConfirmarPassword);
          cadastrar = (Button)findViewById(R.id.btnCadastrar);
          verLogin = (TextView)findViewById(R.id.tvLoginCadastro);
  
          verLogin.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Intent loginIntent = new Intent(Cadastro.this,MainActivity.class);
                  startActivity(loginIntent);
              }
          });
  
          cadastrar.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  String usuario = cadastroNome.getText().toString().trim();
                  String password = cadastroPassword.getText().toString().trim();
                  String cnfPassword = cnfCadastroPassword.getText().toString().trim();
  
                  if(password.equals(cnfPassword)){
                      long val = db.adicionarUsuario(usuario, password);
                      if(val &gt; 0 ){
                      Toast.makeText(Cadastro.this, "Usuário cadastrado", Toast.LENGTH_SHORT).show();
                      Intent irParaLogin = new Intent(Cadastro.this, MainActivity.class);
                      startActivity(irParaLogin);
                  }else{
                          Toast.makeText(Cadastro.this, "Erro no registro", Toast.LENGTH_SHORT).show();
                      }
                      Toast.makeText(Cadastro.this, "A senha não é igual nos dois campos", Toast.LENGTH_SHORT).show();
                  }
              }
          });
  
          }
  
      }
  </code></pre>
  
  <p>And this is the error I get:</p>
  
  <pre><code>    --------- beginning of crash
  09-21 02:29:43.649 5617-5617/com.example.gerenciadorestoqueoficial E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.gerenciadorestoqueoficial, PID: 5617
      java.lang.NullPointerException: Attempt to invoke virtual method 'long com.example.gerenciadorestoqueoficial.DbHelper.adicionarUsuario(java.lang.String, java.lang.String)' on a null object reference
          at com.example.gerenciadorestoqueoficial.Cadastro$2.onClick(Cadastro.java:53)
          at android.view.View.performClick(View.java:4780)
          at android.view.View$PerformClick.run(View.java:19866)
          at android.os.Handler.handleCallback(Handler.java:739)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:135)
          at android.app.ActivityThread.main(ActivityThread.java:5254)
          at java.lang.reflect.Method.invoke(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:372)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
  09-21 02:29:49.585 5617-5637/com.example.gerenciadorestoqueoficial D/EGL_emulation: eglMakeCurrent: 0xaec34ac0: ver 2 0 (tinfo 0xaec393a0)
  09-21 02:34:43.736 5617-5617/com.example.gerenciadorestoqueoficial I/Process: Sending signal. PID: 5617 SIG: 9
  
  </code></pre>