mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
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:
parent
0ade0705e2
commit
bf36dcb648
1 changed files with 13 additions and 2 deletions
|
|
@ -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 =
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue