光标指向null

时间:2017-02-25 14:34:58

标签: android sqlite

我正在Android应用程序中实现一个基本的CRUD函数。我正在通过我的注册创建一个CREATE方法,并在我的登录名上阅读。我没有在注册时收到错误,但是当我登录我的应用程序崩溃并在我的光标上生成空指针异常时。

我点击登录按钮调用方法arr: 0xbf843e28 *arr: 9 arr[0]: 9 p: 0xbf843e28 *p: 9 p[0]: 9 arrPtr: 0xbf843e28 *arrPtr: 0xbf843e28 arrPtr[0]: 0xbf843e28 arrRef: 0xbf843e28 *arrRef: 9 arrRef[0]: 9

searchRecord

以下是我的DBase类中的 mLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(!mUsername.getText().toString().isEmpty() && !mPassword.getText().toString().isEmpty()) { DBase db = new DBase(MainActivity.this); if(db.searchRecord(mUsername.getText().toString(), mPassword.getText().toString())){ Toast.makeText(MainActivity.this, "Welcome, "+mUsername.getText().toString()+"!", Toast.LENGTH_LONG).show(); Intent intent = new Intent(MainActivity.this,TimeSetter.class); startActivity(intent); } else { Intent intent = new Intent(MainActivity.this,SignupActivity.class); startActivity(intent); } } } }); 方法:

searchRecord

我实际上是基于我发现的一个单独项目的get记录函数,因为我并不熟悉SQLite:

    public Boolean searchRecord(String username, String password) throws SQLException{
    Boolean isUser = false;
    String selectQuery = "SELECT * FROM "+DB_TABLE+" WHERE "+K_NAME+" = "+username+ " AND "+K_PASSWORD+" = "+password;
    Cursor c = null;

    c = dBase.rawQuery(selectQuery, null);

    if(c.moveToFirst()){
            isUser = false;
    }
    else {
        isUser = false;
    }
    return isUser;
}

最后,这是我的数据库声明:

    public String[] getRecord(int rid) throws SQLException{
    String selectQuery = "SELECT * FROM "+DB_TABLE+" WHERE "+K_RID+" = "+rid;
    Cursor c = null;
    c = dBase.rawQuery(selectQuery, null);
    String[] data = new String[2];
    if(c.moveToFirst()){
        int indexName = c.getColumnIndex(K_NAME);
        int indexCourse = c.getColumnIndex(K_EMAIL);
        int indexPassword = c.getColumnIndex(K_PASSWORD);
        data[0] = c.getString(indexName);
        data[1] = c.getString(indexCourse);
        data[2] = c.getString(indexPassword);
    }
    return data;
}

以下是关于错误的日志:

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(
                "CREATE TABLE " + DB_TABLE + " (" +
                        K_RID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                        K_NAME + " TEXT NOT NULL, " +
                        K_EMAIL + " TEXT NOT NULL, " +
                        K_PASSWORD + " TEXT NOT NULL);"
        );
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXIST " + DB_TABLE);
        onCreate(db);
    }

它指向这一行: 02-25 22:33:22.261 21810-21810/com.exceptions.finalproject E/AndroidRuntime: FATAL EXCEPTION: main Process: com.exceptions.finalproject, PID: 21810 java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference at com.exceptions.finalproject.DBase.searchRecord(DBase.java:84) at com.exceptions.finalproject.MainActivity$1$1.onClick(MainActivity.java:38) at android.view.View.performClick(View.java:4848) at android.view.View$PerformClick.run(View.java:20262) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5637) 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:960) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

谢谢!

1 个答案:

答案 0 :(得分:1)

你误解了这个例外。该错误表示在空引用上调用方法rawQuery。这意味着dBase变量为null,而不是游标。

问题的根源在于其他地方,无论在哪里初始化该变量。