DatabaseUtils.kt: change getString() to allow null returns

Before this change, a call to getString() would assume that the specified column
name actually exists. A bad String input would cause a null value to be returned
to getString(), which would then throw a NPE because getString() can only return
non null Strings.

This change expands the getString() method to check if the column name exists.
If it does exist, the String is retrieved normally. Else, a null value is
returned. The method signature is changed to allow null return values.
This commit is contained in:
Jason Whitmore 2025-10-08 14:07:22 -07:00
parent 0ade0705e2
commit bf36dcb648

View file

@ -6,9 +6,20 @@ import android.database.Cursor
fun Cursor.getStringArray(name: String): List<String> =
stringToArray(getString(name))
/**
* Gets the String at the current row and specified column.
*
* @param name The name of the column to get the String from.
* @return The String if the column exists. Else, null is returned.
*/
@SuppressLint("Range")
fun Cursor.getString(name: String): String =
getString(getColumnIndex(name))
fun Cursor.getString(name: String): String? {
val index = getColumnIndex(name)
if (index == -1) {
return null
}
return getString(index)
}
@SuppressLint("Range")
fun Cursor.getInt(name: String): Int =