60

Would anyone know how I can view what a cursor has in it during debugging so that I can determine the functionality of my database helper?

It keeps acting like it's returning data, but then when I attempt to use the cursor.isNull(0) method, I keep getting NullPointerException thrown and not being able to see what the cursor has in it while stepping through the execution is really frustrating me.

Any help would be extremely appreciated.

Thanks.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Skittles
  • 2,866
  • 9
  • 31
  • 37

2 Answers2

314

Android has provided a specific class just for debugging Cursors. It is called DatabaseUtils.

Call the method DatabaseUtils.dumpCursorToString(cursor) to view the contents of your cursor.

This helper loops through and prints out the content of the Cursor for you, and returns the cursor to its original position so that it doesn't mess up your iterating logic.

soshial
  • 5,906
  • 6
  • 32
  • 40
Some Noob Student
  • 14,186
  • 13
  • 65
  • 103
-9

If that's null pointer exception, it seems your cursor really is null.

I would use Log.d() to help debug my cursors, you can simply create a helper method to dump the whole row of your cursor to LogCat.

xandy
  • 27,357
  • 8
  • 59
  • 64
  • Okay...so, when I try doing this; Log.d("Activity.cursor", "col 0: " + cursor.getLong(0)); It crashes. When I try this; Log.d("Activity.cursor", "col 0: " + cursor.getCount()); It says col 0: 1 How can it be null? – Skittles Nov 21 '10 at 02:05
  • 35
    I think I found the answer I needed. There is apparently this really awesome class that lets me see the exact cursor contents in the form of a hashtable. DatabaseUtils.dumpCursor(cursor); Thanks for the help anyway, xandy. :) – Skittles Nov 21 '10 at 02:30
  • 4
    Skittles! Your suggestion to use the DatabaseUtils.dumpCursor(cursor) method (or Log.d("TAG", DatabaseUtils.dumpCursorToString(cursor))) is really great, and I would say an even better answer than the currently accepted one! I think you should answer this question yourself, and pick that as the accepted answer. I would vote it up! :) – Eric Hansander Sep 25 '11 at 10:41