在匹配用户标识和密码时获取null异常

时间:2013-10-30 05:55:26

标签: android sqlite

我正在创建一个基于登录活动的应用。当我正在进行注册(注册)时,所有数据都成功进入数据库,但是当用户处于Login Activity并尝试登录时,在Exception中获取null。

这是我的代码,

        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)

我哪里出错?

请帮助我,

感谢。

5 个答案:

答案 0 :(得分: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;
}

必须在返回密码之前关闭光标

答案 1 :(得分:0)

我认为

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

不正确。 也许你应该改为

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

答案 2 :(得分:0)

在创建表语句中,您尚未指定用户ID的类型。

答案 3 :(得分: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;

答案 4 :(得分:0)

解决问题:)

我得到了答案,

我没有写过这个,

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

谢谢大家的帮助。