我正在尝试使用Realm数据库登录和注册用户。但是我得到的错误如下:
尝试在空对象引用上调用虚方法'void io.realm.Realm.beginTransaction()'
是的,有人能帮帮我吗?以下是我的注册和登录代码..
注册代码:
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();
}
}
}
以下是我的登录代码..
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<MyRealmObject> realmObjects = realm.where(MyRealmObject.class).findAll();
for (MyRealmObject myRealmObject : realmObjects) {
if (email.equals(myRealmObject.getEmail()) && 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();
}
}
});
}
我的RealmObject类:
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;
}
}
有人指导我哪里错了。
提前致谢..
答案 0 :(得分:0)
请注意,不推荐使用Realm.getInstance(context)
。您应该在应用程序类中设置默认的RealmConfiguration,并使用Realm.getDefaultInstance()
,如下所述:https://realm.io/docs/java/latest/#controlling-the-lifecycle-of-realm-instances
从快速浏览一下,你的代码不应该抛出异常,但是在try / catch中包装realm = Realm.getInstance(getApplicationContext())
是危险的,因为如果方法抛出,你将得到你看到的NullPointer。我会删除它。