Strange nullPointerException error - Database related

im learning how to work with sqlite database and i tried to implement the highscore system in the game i made. reading the lines works when I try to show the scores, but when I try to use the same idea to read the values ​​to compare them in the following method - it gives a null pointer exception error.

private void checkIfHighScore(int currentScore) {
    Cursor note;
    int rowScore;
    String moveName, moveScore, newName;

    for (long i=1;i<6;i++){
        note = mDbHelper.fetchNote(i);
        startManagingCursor(note);
        rowScore=Integer.parseInt(note.getString(note.getColumnIndexOrThrow(ScoresDbAdapter.KEY_BODY)));
        if (currentScore<rowScore){
            for (long j=5;j>i;j--){
                note = mDbHelper.fetchNote(j-1);
                startManagingCursor(note);
                moveName=note.getString(note.getColumnIndexOrThrow(ScoresDbAdapter.KEY_TITLE));
                moveScore=note.getString(note.getColumnIndexOrThrow(ScoresDbAdapter.KEY_BODY));
                mDbHelper.updateNote(j, moveName, moveScore);
            }
            newName="a"; //for testing 
            mDbHelper.updateNote(i, newName, Integer.toString(currentScore));
            break;
        }
    }

}

      

I know the error is on the line after the for loop, but I have no idea what is causing it. my database contains 5 rows of data (tested). does anyone know what is causing my error? banging my head against the wall for 2 hours. thank!

+3


source to share


1 answer


Alternatively to your code, you can do something like

String selectQuery = "SELECT MAX(COLUMN_NAME) FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst(){
   rowScore = Integer.parseInt(cursor.getString(0);
   if(rowScore < currentScore){
       //you have a new high score
    }
}

      



This way you use the predefined functions that come with SQLite, which are faster and save you some work.

0


source







All Articles