2013-10-30 67 views
1

我正在創建基於登錄活動的應用程序。當我在註冊(註冊)時,所有的數據都成功地進入數據庫,但是當用戶處於登錄活動並嘗試登錄時,然後在Exception中獲得一個空值。在匹配用戶標識和密碼中獲得空例外

這是我的代碼,

 loginuserid=etloginuserid.getText().toString(); 
     loginpassword=etloginpassword.getText().toString(); 
     try 
     { 
      String storedPassword=db.getPasswordOfThisUser(loginuserid); 

      if(loginpassword.equals(storedPassword)) 
      { 
       Toast.makeText(getApplicationContext(), "Login Successfull", Toast.LENGTH_SHORT).show(); 
       Intent iwelpage=new Intent(this,WelcomeActivity.class); 
       startActivity(iwelpage); 
       finish(); 
       break; 
      } 
      else 
      { 
       Toast.makeText(getApplicationContext(), "Username or Password does not match!", Toast.LENGTH_SHORT).show(); 
      } 
     } 
     catch(Exception e) 
     { 
      Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show(); 
     } 

,這是我的數據庫類的活動,

創建表時,

public void onCreate(SQLiteDatabase db) 
    { 
     try 
     { 
      String CREATE_POLLINGDATA_TABLE="CREATE TABLE pollingdata(username TEXT, userid PRIMARY KEY, password TEXT)"; 
      db.execSQL(CREATE_POLLINGDATA_TABLE); 
     } 
     catch(SQLException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

當檢索所輸入的用戶ID的密碼,

public String getPasswordOfThisUser(String loginuserid) 
{ 
    Cursor cursor=db.query("pollingdata", null, " userid=?" , new String[]{loginuserid}, null, null, null); 
    if(cursor.getCount()<1) 
    { 
     cursor.close(); 
     return "Not Exist"; 
    } 
    cursor.moveToFirst(); 
    String password= cursor.getString(cursor.getColumnIndex(KEY_PASSWORD)); 
    return password; 
} 

LogCat,

10-30 06:03:13.066: E/AndroidRuntime(13692): FATAL EXCEPTION: main 
10-30 06:03:13.066: E/AndroidRuntime(13692): java.lang.NullPointerException 
10-30 06:03:13.066: E/AndroidRuntime(13692): at com.example.polling.LoginActivity.onClick(LoginActivity.java:47) 
10-30 06:03:13.066: E/AndroidRuntime(13692): at android.view.View.performClick(View.java:4204) 
10-30 06:03:13.066: E/AndroidRuntime(13692): at android.view.View$PerformClick.run(View.java:17355) 
10-30 06:03:13.066: E/AndroidRuntime(13692): at android.os.Handler.handleCallback(Handler.java:725) 
10-30 06:03:13.066: E/AndroidRuntime(13692): at android.os.Handler.dispatchMessage(Handler.java:92) 
10-30 06:03:13.066: E/AndroidRuntime(13692): at android.os.Looper.loop(Looper.java:137) 
10-30 06:03:13.066: E/AndroidRuntime(13692): at android.app.ActivityThread.main(ActivityThread.java:5041) 
10-30 06:03:13.066: E/AndroidRuntime(13692): at java.lang.reflect.Method.invokeNative(Native Method) 
10-30 06:03:13.066: E/AndroidRuntime(13692): at java.lang.reflect.Method.invoke(Method.java:511) 
10-30 06:03:13.066: E/AndroidRuntime(13692): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
10-30 06:03:13.066: E/AndroidRuntime(13692): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
10-30 06:03:13.066: E/AndroidRuntime(13692): at dalvik.system.NativeStart.main(Native Method) 

我在哪裏出錯?

請幫我,

謝謝。

+1

您可以發佈異常和堆棧跟蹤以獲取更多信息。 – Kenneth

+0

你能清楚地提到你在哪裏得到空指針exception..i.e,活動或數據庫 –

+0

'String storedPassword = db.getPasswordOfThisUser(loginuserid);'這裏 – Shiv

回答

0

問題解決了:)

我有我的回答,

我沒有寫這個,

db=new DBAdapter(LoginActivity.this); 
db.open(); 
String storedPassword=db.getPasswordOfThisUser(loginuserid); 
db.close(); 

謝謝大家的幫助。

+0

你的db沒有初始化,所以你得到NPE – Raghunandan

+0

@Raghunandan:hmm..yeah :) – Shiv

0

我覺得

"CREATE TABLE pollingdata(username TEXT, userid PRIMARY KEY, password TEXT)" 

不正確。 也許你應該改變

"CREATE TABLE pollingdata(username TEXT, userid INTEGER PRIMARY KEY, password TEXT)" 
+0

如果'PRIMARY KEY'是'TEXT'會怎麼樣? – Shiv

1

試試這個

public String getPasswordOfThisUser(String loginuserid) { 
    Cursor cursor = db.query("pollingdata", new String[] { KEY_PASSWORD }, 
      " userid=?", new String[] { username }, null, null, null); 
    if (cursor.getCount() < 1) { 
     cursor.close(); 
     return "Not Exist"; 
    } 
    cursor.moveToFirst(); 
    String password = cursor.getString(cursor.getColumnIndex(KEY_PASSWORD)); 
    return password; 
} 

必須返回密碼

0

在塔建立你沒有指定用戶ID的類型TABLE語句之前關閉遊標。

0

變化

if(cursor.getCount()<1) 
{ 
    cursor.close(); 
    return "Not Exist"; 
} 
cursor.moveToFirst(); 

String password=""; 
if(cursor!=null) 
{ 
    cursor.moveToFirst(); 
    password= cursor.getString("password"); 
} 
return password; 
相關問題