我正在創建基於登錄活動的應用程序。當我在註冊(註冊)時,所有的數據都成功地進入數據庫,但是當用戶處於登錄活動並嘗試登錄時,然後在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)
我在哪裏出錯?
請幫我,
謝謝。
您可以發佈異常和堆棧跟蹤以獲取更多信息。 – Kenneth
你能清楚地提到你在哪裏得到空指針exception..i.e,活動或數據庫 –
'String storedPassword = db.getPasswordOfThisUser(loginuserid);'這裏 – Shiv