0
當我試圖從SQLite的獲取用戶名和密碼,我發現以下異常無法從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("");
}
}
顯示您的代碼,請 –
顯示' DatabaseOpenHelper'類代碼 –
'java.lang.NullPointerException' –