I have been searching for an answer to this problem, and found a number of other similar questions. However, I believe I followed the code in the accepted answers and I'm still throwing a NullPointerException error when I try to run it. The question I looked at was this. This was the code suggested for getting a column value from a cursor:
String id = cursor.getString( cursor.getColumnIndex("id") );
The answer made sense to me so I adapted it for my own function:
public String getSubjectId(String subject, String level) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor subjectCursor = db.rawQuery("SELECT * FROM " + SUBJECTS_TABLE_NAME + " WHERE " + SUBJECTS_COL_2 + " = " + subject + " AND " + SUBJECTS_COL_3 + " = " + level, null);
String id = subjectCursor.getString(subjectCursor.getColumnIndex("ID"));
return id;
}
When I run this I am throwing a NullPointerException error. I can't see a problem with the code and am confused about what the error means here. My understanding of the error is that it occurs when a variable is declared but it does not create an object, but I can't see where I have done that in my code. An explanation of what I have done wrong to trigger this error would be much appreciated, as I believe I have followed a number of online tutorials accurately.
Edit: I'm aware of this question but my issue is I can't see a place where I am assigning an object as null so the solution isn't helping me.