Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid NullPointerException when accessing SQLite DB
    text
    copied!<p>I need help guys! I have a bug which I've been working on for hours and can't solve.</p> <p>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...</p> <pre><code>public Game getGame(gameid){ Long gameif = Long.parseLong(gameid); Game g = db.getGame(gameif); return g; } </code></pre> <p>The line Game g = db.getGame() is the one giving me the NPE. The incoming gameid is not null, the reference to the database <code>db = new DatabaseHandler(this);</code> isn't null (I don't think).</p> <p>Here's the code from the DatabaseHandler:</p> <pre><code>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; } </code></pre> <p>Thanks in advance!</p> <p>Edit - Here's the logcat.</p> <pre><code>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) </code></pre> <p>Click on button handled in my <code>customadapter</code> then starts <code>talktoasync()</code> 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. <code>getGame()</code> is triggered in the <code>onPostExecute()</code> of the asynctask.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload