0

I am implementing a basic CRUD function in an android application. I am creating a CREATE method through my registration and READ on my login. I'm not receiving errors on my registration but when I login my app crashes and generates a null pointer exception on my cursor.

I call the method searchRecord on button click of login:

    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);
                        }

                    }
                }
            });

Here is the searchRecord method in my DBase class:

    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;
}

I actually based that on a get record function from a separate project that I found since I'm not really familiar with SQLite:

    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;
}

And lastly, here is my database declaration:

    @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);
    }

Here is my logs about the error:

    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)

It points to this line: c = dBase.rawQuery(selectQuery, null);

Thanks!

Paradigm
  • 159
  • 1
  • 3
  • 11

1 Answers1

1

You are misreading the exception. The error says the method rawQuery is being called on a null reference. This means the dBase variable is null, not the cursor.

The root of your problem lies elsewhere, wherever that variable is initialized.

Kiskae
  • 24,655
  • 2
  • 77
  • 74