I need help guys! I have a bug which I've been working on for hours and can't solve.
I have a ListView and customadapter, listview is populated with objects from a local sqlite db. I have a button on each row of the listview which is supposed to delete the corresponding object from the database. However, when the button is clicked I query the database like so...
public Game getGame(gameid){
Long gameif = Long.parseLong(gameid);
Game g = db.getGame(gameif);
return g;
}
The line Game g = db.getGame() is the one giving me the NPE. The incoming gameid is not null, the reference to the database db = new DatabaseHandler(this); isn't null (I don't think).
Here's the code from the DatabaseHandler:
Game getGame(Long id) {
SQLiteDatabase db = this.getReadableDatabase();
Game game= null;
System.out.println(String.valueOf(id));
Cursor cursor = db.query(TABLE_GAMES, new String[] { KEY_ID,
KEY_PLAYERNAME, KEY_PLAYERNUMBER, KEY_LASTTURN, KEY_NEXTTURN }, KEY_ID + "= ?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null){
cursor.moveToFirst();
game = new Game(Long.parseLong(cursor.getString(0)), cursor.getString(1),
Integer.parseInt(cursor.getString(2)),
cursor.getString(3), Integer.parseInt(cursor.getString(4)));
}
return game;
}
Thanks in advance!
Edit - Here's the logcat.
09-29 11:06:52.843: E/AndroidRuntime(16969): FATAL EXCEPTION: main
09-29 11:06:52.843: E/AndroidRuntime(16969): java.lang.NullPointerException
09-29 11:06:52.843: E/AndroidRuntime(16969): at com.domsoft.ff.MainActivity.getGame(MainActivity.java:115)
09-29 11:06:52.843: E/AndroidRuntime(16969): at com.domsoft.ff.MainActivity.talkToAsync(MainActivity.java:71)
09-29 11:06:52.843: E/AndroidRuntime(16969): at com.domsoft.ff.GameCustomAdapter$1.onClick(GameCustomAdapter.java:72)
09-29 11:06:52.843: E/AndroidRuntime(16969): at android.view.View.performClick(View.java)
09-29 11:06:52.843: E/AndroidRuntime(16969): at android.view.View$PerformClick.run(View.java)
09-29 11:06:52.843: E/AndroidRuntime(16969): at android.os.Handler.handleCallback(Handler.java)
09-29 11:06:52.843: E/AndroidRuntime(16969): at android.os.Handler.dispatchMessage(Handler.java)
09-29 11:06:52.843: E/AndroidRuntime(16969): at android.os.Looper.loop(Looper.java)
09-29 11:06:52.843: E/AndroidRuntime(16969): at android.app.ActivityThread.main(ActivityThread.java)
09-29 11:06:52.843: E/AndroidRuntime(16969): at java.lang.reflect.Method.invokeNative(Native Method)
09-29 11:06:52.843: E/AndroidRuntime(16969): at java.lang.reflect.Method.invoke(Method.java)
09-29 11:06:52.843: E/AndroidRuntime(16969): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
09-29 11:06:52.843: E/AndroidRuntime(16969): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
09-29 11:06:52.843: E/AndroidRuntime(16969): at dalvik.system.NativeStart.main(Native Method)
Click on button handled in my customadapter then starts talktoasync() in the main activity, an asynctask is then run which sends some data to a server, then depending on the response code from the server, a record from the local db should be deleted. getGame() is triggered in the onPostExecute() of the asynctask.