title: Registration and Login Using Realm
body: <p>I am trying to do login and registration of user using Realm database. However am getting a bug as follows:</p>
  
  <p><strong>Attempt to invoke virtual method 'void io.realm.Realm.beginTransaction()' on a null object reference</strong> </p>
  
  <p>Can someone help me please?  </p>
  
  <p>Below is my code for my registration and login.. </p>
  
  <p><strong>Code for Registration:</strong></p>
  
  <pre><code>public class RegisterActivity extends AppCompatActivity {
  
  private static final String TAG = "Register";
  EditText mEditTextFirstName, mEditTextLastName, mEditTextEmail,mEditTextPassword,mEditTextConfirmPassword;
  Button mButtonSignUp;
  Realm realm;
  private MyRealmObject myRealmObject;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_register);
  
      try {
          realm = Realm.getInstance(getApplicationContext());
      } catch (Exception e) {
          e.printStackTrace();
      }
  
      mEditTextFirstName = (EditText) findViewById(R.id.edtfirstname);
      mEditTextLastName= (EditText) findViewById(R.id.edtlastname);
      mEditTextEmail = (EditText) findViewById(R.id.edtemail);
      mEditTextPassword = (EditText) findViewById(R.id.edtpassword);
      mEditTextConfirmPassword = (EditText) findViewById(R.id.edtconfirm);
      mButtonSignUp = (Button) findViewById(R.id.btnsignup);
  
  
      mButtonSignUp.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              if (mEditTextFirstName.length() == 0) {
                  showSnackBar("Enter FirstName");
                  mEditTextFirstName.requestFocus();
              } else if (mEditTextLastName.length() == 0) {
                  showSnackBar("Enter LastName");
                  mEditTextEmail.requestFocus();
              } else if (mEditTextEmail.length() ==0) {
                  showSnackBar("Enter a valid email");
                  mEditTextEmail.requestFocus();
              }else if (mEditTextPassword.length() ==0) {
                  showSnackBar("Enter a valid password");
                  mEditTextPassword.requestFocus();
              }else if (mEditTextConfirmPassword.length() ==0) {
                          showSnackBar("Enter a valid Password");
                          mEditTextPassword.requestFocus();
              } else {
  
                  try{
  
                      realm.beginTransaction();
  
                      myRealmObject = realm.createObject(MyRealmObject.class);
                      myRealmObject.setFirstName(mEditTextFirstName.getText().toString());
                      myRealmObject.setLastName(mEditTextLastName.getText().toString());
                      myRealmObject.setEmail(mEditTextEmail.getText().toString());
                      myRealmObject.setPassword(mEditTextPassword.getText().toString());
                      myRealmObject.setConfirmPassword(mEditTextConfirmPassword.getText().toString());
  
  
                      realm.commitTransaction();
  
                      showSnackBar("Save Success");
  
  
                  } catch (RealmPrimaryKeyConstraintException e){
                      e.printStackTrace();
                      showSnackBar("User found on db.");
                  }
  
              }
          }
      });
  
  
  }
  
  private void showSnackBar(String msg) {
      try {
          Snackbar.make(findViewById(R.id.relativeregister), msg, Snackbar.LENGTH_SHORT).show();
      } catch (NullPointerException e) {
          e.printStackTrace();
          Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
      }
  }
  
  
  
  }
  </code></pre>
  
  <p><strong>Here is my code for Login..</strong></p>
  
  <pre><code>public class LoginActivity extends AppCompatActivity {
  
  private static final String TAG = "Home";
  private EditText mEditTextEmail, mEditTextPassword;
  private TextView txtlogin;
  TextView Create;
  private Realm realm;
  private MyRealmObject myRealmObject;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_login);
  
      try {
          realm = Realm.getInstance(getApplicationContext());
      } catch (Exception e) {
          e.printStackTrace();
      }
  
      mEditTextEmail = (EditText) findViewById(R.id.edtemail);
      mEditTextPassword = (EditText) findViewById(R.id.edtpswrd);
      txtlogin = (TextView) findViewById(R.id.txtlogin);
      Create = (TextView) findViewById(R.id.txtcreate);
  
  
      Create.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
  
              Intent i = new Intent(LoginActivity.this, RegisterActivity.class);
              startActivity(i);
  
          }
      });
  
      txtlogin.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              if (mEditTextEmail.length() == 0) {
                  showSnackBar("Enter EMAIL");
                  mEditTextEmail.requestFocus();
              } else if (mEditTextPassword.length() == 0) {
                  showSnackBar("Enter password");
                  mEditTextPassword.requestFocus();
                  Intent i = new Intent(LoginActivity.this, MainActivity.class);
                  startActivity(i);
  
  
              }
          }
  
           private boolean checkUser(String email,String password) {
                  RealmResults&lt;MyRealmObject&gt; realmObjects = realm.where(MyRealmObject.class).findAll();
                 for (MyRealmObject myRealmObject : realmObjects) {
                     if (email.equals(myRealmObject.getEmail()) &amp;&amp; password.equals(myRealmObject.getPassword())) {
                          Log.e(TAG, myRealmObject.getEmail());
                          return true;
                      }
      }
  
  
                  Log.e(TAG, String.valueOf(realm.where(MyRealmObject.class).contains("Email", email)));
                 return false;
            }
  
  
  
          private void showSnackBar(String msg) {
              try {
                  Snackbar.make(findViewById(R.id.relativelogin), msg, Snackbar.LENGTH_SHORT).show();
              } catch (NullPointerException e) {
                  e.printStackTrace();
                  Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
              }
          }
  
  
      });
  }
  </code></pre>
  
  <p><strong>My RealmObject Class:</strong></p>
  
  <pre><code>public class MyRealmObject extends RealmObject {
  private String FirstName, LastName,ConfirmPassword;
  @PrimaryKey
  private String Email;
  private String Password;
  
  public MyRealmObject() {
  }
  
  public String getFirstName() {
      return FirstName;
  }
  
  public void setFirstName(String FirstName) {
      this.FirstName = FirstName;
  }
  
  public String getLastName() {
      return LastName;
  }
  
  public void setLastName(String LastName) {
      this.LastName = LastName;
  }
  
  public String getEmail() {
      return Email;
  }
  
  public void setEmail(String email) {
      this.Email = Email;
  }
  
  public String getPassword() {
      return Password;
  }
  
  public void setPassword(String password) {
      this.Password = password;
  }
  
  public String getConfirmPassword() {
      return getConfirmPassword();
  }
  
  public void setConfirmPassword(String confirmPassword) {
      this.ConfirmPassword = confirmPassword;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Someone guide me where am wrong.</p>
  
  <p>Thanks in advance.. </p>