0

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.

3
  • Debug and check if your cursor is null, you do a not equal to null check, but then use the cursor in the next line Commented Sep 29, 2013 at 0:38
  • Please post your logcat, it will help us to help you. Commented Sep 29, 2013 at 3:42
  • Please see the edit! Also thanks for your help, I'll start trying all the tips mentioned in this thread now. Commented Sep 29, 2013 at 10:14

1 Answer 1

1

When you acces at the Data acces object, you first need have and instance of the object

-> db = new DatabaseHandler(this);

after you need open the database where you need to acces.

db.open() ;

Set this segment of code in your Handler:

private MySQLiteHelper dbHelper; 

   public void open() throws SQLException {

    db = dbHelper.getWritableDatabase();

    }

If you dont open the database Android throws NullPointerException . YOu have a problem when you opening the database and this throws the exeption.

You can see this tutorial for more information : http://www.vogella.com/articles/AndroidSQLite/article.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.