当我尝试从Sqlite检索用户名和密码时,我发现了以下异常
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.nawin.demo.database.DatabaseOpenHelper.getUserPass(DatabaseOpenHelper.java:115)
at com.example.nawin.demo.login_fragment$2.onClick(login_fragment.java:68)
at android.view.View.performClick(View.java:5076)
at android.view.View$PerformClick.run(View.java:20279)
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:5910)
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:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
//这是我的方法来检索
public void getUserPass(String user, String pass)throws SQLException{
db=this.getWritableDatabase();
String query="select * from register where username= '"+user+"' and password= '"+pass+"'";
cursor=db.rawQuery(query,null);
}
//这是我在上面调用的方法
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String strUser = txUserName.getText().toString();
String strPass = txPassword.getText().toString();
boolean loginStatus = false;
String NAME = "";
if (view.getId() == R.id.btnLogin) {
helper.getUserPass(strUser,strPass);
helper.cursor.moveToFirst();
do {
if(strUser.equals(helper.cursor.getString(0))&& (strPass.equals(helper.cursor.getString(1)))){
loginStatus=true;
NAME=helper.cursor.getString(0);
}
}while (helper.cursor.moveToNext());
if(loginStatus==true){
Toast.makeText(getActivity(),"Login Successful!!",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getActivity(),"Invalid Username Password",Toast.LENGTH_SHORT).show();
txUserName.setText("");
txPassword.setText("");
}
}
答案 0 :(得分:0)
loginButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String userName = txUserName.getText().toString();
String password = txPassword.getText().toString();
String storedPassword = helper
.getSinlgeEntry(userName);
if (password.equals(storedPassword)) {
// ur stuff
}
}
});
将你的getUserPass(字符串用户,字符串传递)更改为此并修改你的代码尝试以下操作:
public String getSinlgeEntry(String userName) {
db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("REGISTER", null, " USERNAME=?",
new String[] { userName }, null, null, null);
if (cursor.getCount() < 1) {
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String password = cursor.getString(cursor.getColumnIndex("PASSWORD"));
cursor.close();
return password;
}
或者发布db类的完整代码我们将检查