Viewing an Android database cursor

AndroidDebuggingDatabase Cursor

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSkittlesView Question on Stackoverflow
Solution 1 - AndroidSome Noob StudentView Answer on Stackoverflow
Solution 2 - AndroidxandyView Answer on Stackoverflow