2016-01-04 119 views
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(""); 
      } 


     } 
+2

顯示您的代碼,請 –

+0

顯示' DatabaseOpenHelper'類代碼 –

+0

'java.lang.NullPointerException' –

回答

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類的完整代碼中,我們會檢查