Reputation: 6209
I can't understand why the query ends up throwing a NullPointerException
.
My database contains two tables. The first one called TABLE_WORDS
contains words; the second one called TABLE_TRANS
contains the translations. One word can have many translations.
public static final String CREATE_TABLE_WORDS = "CREATE TABLE " + TABLE_WORDS +
" (" + WORDS_ID + " INTEGER PRIMARY KEY, " + WORDS_WORD +
" TEXT);";
public static final String CREATE_TABLE_TRANS = "CREATE TABLE " + TABLE_TRANS +
" (" + TRANS_ID + " INTEGER PRIMARY KEY, " +
TRANS_WORD + " TEXT, " +
TRANS_FOREIGN_ID_WORD + " INTEGER, " +
"FOREIGN KEY (" + TRANS_FOREIGN_ID_WORD + ") REFERENCES " + TABLE_WORDS + "(" + WORDS_ID + "));";
I query translations by a word ID, and an NPE
is thrown.
public Cursor queryTransesByWord(int idSend) {
String query = "SELECT " + DbHelper.TABLE_WORDS + "." + DbHelper.WORDS_WORD + ", " +
DbHelper.TABLE_TRANS + "." + DbHelper.TRANS_WORD +
" FROM " + DbHelper.TABLE_WORDS +
" INNER JOIN " + DbHelper.TABLE_TRANS +
" ON " + DbHelper.TABLE_WORDS + "." + DbHelper.WORDS_ID + "=" +
DbHelper.TABLE_TRANS + "." + DbHelper.TRANS_FOREIGN_ID_WORD + " WHERE " +
DbHelper.TABLE_WORDS + "." + DbHelper.WORDS_ID + "="+ idSend;
Cursor cur = db.rawQuery(query, null); // NullPointer always throws here
if (cur != null)
cur.moveToFirst();
else
Log.e(Constants.LOG_TAG, Constants.DB_ADAPTER_CLASS_NAME_SEP + "queryTransesByWord cursor is null");
return cur;
}
Upvotes: 2
Views: 1327
Reputation: 11
String query = "SELECT " + DbHelper.TABLE_WORDS + "." + DbHelper.WORDS_WORD + ", " +
DbHelper.TABLE_TRANS + "." + DbHelper.TRANS_WORD +
" FROM " + DbHelper.TABLE_WORDS +
" INNER JOIN " + DbHelper.TABLE_TRANS +
" ON " + DbHelper.TABLE_WORDS + "." + DbHelper.WORDS_ID + "=" +
DbHelper.TABLE_TRANS + "." + DbHelper.TRANS_FOREIGN_ID_WORD + " WHERE " +
DbHelper.TABLE_WORDS + "." + DbHelper.WORDS_ID + "=?";
Cursor cur = db.rawQuery(query, new String[] { Integer.toString(idSend) } );
this works for me:-)
Upvotes: 1
Reputation: 6209
In my case I should use
String query = "SELECT " + DbHelper.TABLE_WORDS + "." + DbHelper.WORDS_WORD + ", " +
DbHelper.TABLE_TRANS + "." + DbHelper.TRANS_WORD +
" FROM " + DbHelper.TABLE_WORDS +
" INNER JOIN " + DbHelper.TABLE_TRANS +
" ON " + DbHelper.TABLE_WORDS + "." + DbHelper.WORDS_ID + "=" +
DbHelper.TABLE_TRANS + "." + DbHelper.TRANS_FOREIGN_ID_WORD + " WHERE " +
DbHelper.TABLE_WORDS + "." + DbHelper.WORDS_ID + "=?";
Cursor cur = db.rawQuery(query, new String[] { Integer.toString(idSend) } );
Am I right?
Upvotes: 1
Reputation: 1504062
Well, given your code:
Cursor cur = db.rawQuery(query, null); // NullPointer always throws here
That suggests that db
is null. There's no other dereferencing operation in that statement - so unless the stack trace actually shows rawQuery
in it somewhere, then db
is null. You haven't given any indication of where db
is meant to be assigned a non-null value, but that's the first thing to check...
As an aside, please don't include values in your SQL like that - it's a recipe for SQL injection attacks. Use parameterized SQL instead.
Upvotes: 4